日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

算法(8)-leetcode-explore-learn-数据结构-链表

發布時間:2023/12/13 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 算法(8)-leetcode-explore-learn-数据结构-链表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

leetcode-explore-learn-數據結構-鏈表1

  • 1.概述
    • 1.1 鏈表插入操作
    • 1.2 鏈表刪除操作
  • 2.設計鏈表

本系列博文為leetcode-explore-learn子欄目學習筆記,如有不詳之處,請參考leetcode官網:https://leetcode-cn.com/explore/learn/card/linked-list/

所有例題的編程語言為python

1.概述

鏈表是一種線性結構,每個元素都是一個單獨對象,所有的對象通過每個元素的引用字段鏈接在一起。
鏈表包括單鏈表和雙鏈表,本文主要記錄單鏈表的相關內容。
單鏈表的每個節點包含兩個部分的內容–鏈接到下一個結點的引用字段和值

Python中單鏈表節點的結構

class SinglyListNode(object,val):def __init__(self,val):self.val=valself.next=None

大多數情況下,我們使用頭結點來表示整個列表。

與數組不同,訪問單鏈表的中的元素需要的時間復雜度為0(n),從頭結點開始,往后逐個遍歷。

鏈表的主要優點:插入和刪除操作很便利

1.1 鏈表插入操作

(1)用給定值初始化新結點cur
(2)將cur的Next字段鏈接到prev的下一個結點next
(3)將prev的next字段鏈接到cur結點

相比與數組添加新的元素(需要插入元素后續位置元素往后移動),鏈表插入操作可以實現o(1)的時間復雜度。

在列表的開頭添加新節點更新head關重要。
(1)初始化一個新節點cur
(2)將新節點鏈接到原始head節點指向的下一個結點
(3)更新head節點指向cur

1.2 鏈表刪除操作

要求刪除給定節點cur
(1)找到cur節點的上一個結點prev和下一個結點next;
(2)將pre的next字段鏈接至next結點
找到cur結點的prev結點的時間復雜度為o(n),找到cur的下結點時間復雜度o(1),所以總體時間復雜度為o(n)
空間復雜度為0(1)。

刪除第一個節點:直接將第二個節點分配給head

2.設計鏈表

設計鏈表的實現。單鏈表節點有兩個熟悉:val、next。val是當前節點的值,next是指向下一個結點的指針/引用。
在鏈表中實現:
1.get(index):獲取鏈表的第index個節點,如果索引無效,則返回-1(index 從0開始)
2.addAtHead(val):在鏈表的第一個結點前添加一個值為val的結點。插入后,新節點將成為鏈表的第一個結點
3.addAtTail:將值為val的節點追加到鏈表的最后一個元素
4.addAtIndex(index,val):在鏈表的第index個節點前添加值為val的結點。如果index為鏈表的長度,則將該結點按附加到鏈表的末尾。如果index大于鏈表長度,則不會插入節點。如果index小于0,則在頭部插入節點。
5.deleteAtIndex(index):如果索引有效則刪除鏈表的第index個結點。

class ListNode:def __init__(self,x):self.val=xself.next=Noneclass MyLinkedList(object):def __init__(self):"""Initialize your data structure here."""self.size=0self.head=ListNode(0)def get(self, index):"""Get the value of the index-th node in the linked list. If the index is invalid, return -1.:type index: int:rtype: int"""if index<0 or index>=self.size:return -1node=self.headfor _ in range (index+1): # index 從0開始 node=node.nextreturn node.valdef addAtHead(self, val):"""Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.:type val: int:rtype: None"""self.addAtIndex(0,val)def addAtTail(self, val):"""Append a node of value val to the last element of the linked list.:type val: int:rtype: None"""self.addAtIndex(self.size,val)def addAtIndex(self, index, val):"""Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.:type index: int:type val: int:rtype: None"""if index>self.size:returnif index<0:index=0self.size+=1pred=self.headfor _ in range(index): #[0,index-1]pred=pred.nextnode=ListNode(val)node.next=pred.nextpred.next=nodedef deleteAtIndex(self, index):"""Delete the index-th node in the linked list, if the index is valid.:type index: int:rtype: None"""if index<0 or index>=self.size:returnself.size-=1pred=self.headfor _ in range(index): # [0,index-1]pred=pred.nextpred.next=pred.next.next 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的算法(8)-leetcode-explore-learn-数据结构-链表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。