LeetCode鍊錶總結(待完成)

2022-09-14 10:18:10 字數 4583 閱讀 9516

#

節點class

listnode:

def__init__(self,val = 0,next =none):

self.val =val

self.next =next

#空鍊錶

class

linkedlist:

def__init

(self):

self.head =none

#根據data初始化乙個新鍊錶

defcreate(self,data):

self.head =listnode(0)

cur =self.head

for i in

range(len(data)):

node =listnode(data[i])

cur.next =node

cur =cur.next

#求線性鍊錶長度

deflength(self):

count =0

cur =self.head

while

cur:

count += 1cur =cur.next

return

count

#查詢元素

deffind(self,val):

cur =self.head

while

cur:

if val ==cur.val

return

cur cur =cur.next

return

none

#插入元素

##頭部插入元素

definserfront(self,val):

node =listnode(val)

node.next =self.head

self.head =node

##尾部插入元素

definsertrear(self,val):

node =listnode(val)

cur =self.head

while

cur.next:

cur =cur.next

cur.next =node

##中間插入元素

definsertinside(self,index,val):

count =0

cur =self.head

while cur and count < index - 1:

count += 1cur =cur.next

ifnot

cur:

return

'error

'node =listnode(val)

node.next =cur.next

cur.next =node

#改變元素

defchange(self,index,val):

count =0

cur =self.head

while cur and count count += 1cur =cur.next

ifnot

cur:

return

'error

'cur.val =val

#刪除元素

##刪除頭部元素

defremovefront(self):

ifself.head:

self.head =self.head.next

#刪除尾部元素

defremoverear(self):

ifnot

self.head.next:

return

'error

'cur =self.head

while

cur.next.next:

cur =cur.next

cur.next =none

#中間刪除元素

defremoveinside(self,index):

count =0

cur =self.head

while cur.next and count < index - 1:

count += 1cur =cur.next

ifnot

cur:

return

'error

'del_node =cur.next

cur.next =del_node.next

return-1

707.設計鍊錶

class

listnode:

def__init__

(self, x):

self.val =x

self.next =none

class

mylinkedlist:

def__init__

(self):

self.size =0

self.head =listnode(0)

def get(self, index: int) ->int:

if index < 0 or index >=self.size:

return -1curr =self.head

for i in range(index+1):

curr =curr.next

return

curr.val

def addathead(self, val: int) ->none:

node =listnode(0)

self.head.val =val

node.next =self.head

self.head =node

self.size += 1

def addattail(self, val: int) ->none:

node =listnode(val)

curr =self.head

while

curr.next:

curr =curr.next

curr.next =node

self.size += 1

def addatindex(self, index: int, val: int) ->none:

if index >self.size:

return

if index <0:

index =0

self.size += 1curr =self.head

for i in

range(index):

curr =curr.next

node =listnode(val)

node.next =curr.next

curr.next =node

def deleteatindex(self, index: int) ->none:

if index < 0 or index >=self.size:

return

self.size -= 1curr =self.head

for i in

range(index):

curr =curr.next

curr.next =curr.next.next

206.反轉鍊錶

class

solution:

def reverselist(self, head: listnode) ->listnode:

cur =none

pre =head

while pre !=none:

next =pre.next

pre.next =cur

cur =pre

pre =next

return cur

203.移除鍊錶元素

class

solution:

def removeelements(self, head: listnode, val: int) ->listnode:

cur = head #

有時候也可以改變head。

while cur and

cur.next:

if cur.next.val ==val:

cur.next =cur.next.next

cur =cur.next

return head

328.奇偶鍊錶

class

solution:

def oddevenlist(self, head: listnode) ->listnode:

ifnot

head:

return

head

evenhead =head.next

odd, even =head, evenhead

while even and

even.next:

odd.next =even.next

odd =odd.next

even.next =odd.next

even =even.next

odd.next =evenhead

return head

各種排序演算法總結(待完成)

參考 http blog.csdn.net sturun archive 2008 12 11 3491225.aspx 簡潔,好 http blog.csdn.net yuguanglou archive 2004 11 24 193133.aspx 類模板 參考演算法i iv基礎 資料結構 排序...

鍊錶leetcode總結 python

鍊錶是經典的遞迴定義的資料結構,鍊錶相關的題目常常考察遞迴,翻轉鍊錶是其中的經典題目。在思考遞迴問題的時候,我們要從上到下思考 編寫乙個函式,使其可以刪除某個鍊錶中給定的 非末尾 節點,你將只 被給定要求被刪除的節點 node.val node.next.val node.next node.nex...

LeetCode 鍊錶題總結

最近花了幾天時間,將鍊錶題除帶鎖外的題目做完了,下面對鍊錶題部分題目進行總結和分析。1 鍊錶反轉 2 快慢指標 遇到一些需要找到鍊錶的中點問題時,可能會有鍊錶長度為奇數或者偶數的情況,當長度為偶數時,模板裡面 prev 為第乙個中點,slow 為第二個中點,長度為奇數時 slow 為鍊錶的中點。1....