劍指offer刷題日記 鍊錶

2021-10-05 17:20:14 字數 3926 閱讀 3822

鍊錶03-從尾到頭列印鍊錶(python)

#採用insert方法

class

solution

: def printlist

(self,listnode)

:if not listnode:

return

result=

while listnode:

result.

insert(0

,listnode.val)

listnode=listnode.next

return result

鍊錶014-鍊錶中倒數第k個節點(python)

#採用快慢指標

class

solution

: def findkthtotail

(self,head,k)

:if not head:

return none

fast=head

slow=head

for i in

range

(k):

if fast!=none:

fast=fast.next

else

:return none

while fast:

fast=fast.next

slow=slow.next

return slow

鍊錶015-反轉鍊錶(python)

class

solution

: def reverselist

(self,head)

:if not head:

return none

last=none

while head:

temp=head.next

head.next=last

last=head

head=temp

return last

鍊錶016-合併兩個有序鍊錶(python)

#迭代

class

solution

: def merge

(self,phead1,phead2)

:if phead1=none:

return phead2

if phead2=none:

return phead1

pmergehead=none

if phead1.valpmergehead=phead1

pmergehead=self.

merge

(phead1.next,phead2)

else

: pmergehead=phead2

pmergehead=self.

merge

(phead1,phead2.next)

return pmergehead

鍊錶025-複雜鍊錶的複製(python)

#複製節點,複製random,拆分

class

solution

: def clone

(self,phead)

:if not phead:

return none

pnode=phead

while pnode:

pclone=

randomlistnode

(pnode.label)

pclone.next=pnode.next

pnode.next=pclone

pnode=pclone.next

pnode=phead

while pnode:

pclone=pnode.next

if pnode.random!=none:

pclone.random=pnode.random.next

pnode=pclone.next

pnode=phead

pclonehead=pclonenode=pnode.next

pnode.next=pclonehead.next

while pnode:

pclonenode.next=pnode.next

pclonenode=pclonenode.next

pclone.next=pclonenode.next

pnode=pnode.next

return pclonehead

鍊錶036-兩個鍊錶的第乙個公共節點(python)

class

solution

: def findfirst

(self,phead1,phead2)

:if not phead1 or not phead2:

return none

p1,p2=phead1,phead2

count_1=count_2=

0while phead1:

p1=p1.next

count_1+=

1while phead2:

p2=p2.next

count_2+=

1if count_1>count_2:

while

(count_1-count_2)

: phead1=phead1.next

count_1-=

1else

:while

(count_2-count_1)

: phead2=phead2.next

count_2-=

1while phead1 and phead2:

if phead1.val==phead2.val:

return phead1

phead1=phead1.next

phead2=phead2.next

return none

鍊錶055-鍊錶中的入口節點(python)

#快慢指標

class

solution

: def entrynodeofloop

(self,phead)

:if not phead or not phead.next:

return none

fast=phead.next

slow=phead

while fast!=slow and fast.next:

fast=fast.next.next

slow=slow.next

if fast=slow:

fast=phead

while fast!=slow:

fast=fast.next

slow=slow.next

return fast

return none

鍊錶056-刪除鍊錶中重複節點(python)

class

solution

: def deleteduplication

(self,phead)

: head=

listnode(0

) head.next=phead

pre=head

p=phead

while p and p.next:

if p.val==p.next.val:

while p.next and p.next.val=p.val:

p.next=p.next.next

pre.next=p.next

p=pre.next

else

: pre=p

p=p.next

return head.next

劍指Offer刷題 鍊錶

劍指 offer 18.刪除鍊錶的節點 難度簡單 給定單向鍊錶的頭指標和乙個要刪除的節點的值,定義乙個函式刪除該節點。返回刪除後的鍊錶的頭節點。注意 此題對比原題有改動 示例 1 輸入 head 4,5,1,9 val 5 輸出 4,1,9 解釋 給定你鍊錶中值為 5 的第二個節點,那麼在呼叫了你的...

《劍指offer》刷題(1)鍊錶

最近二刷 劍指offer 的題目,寫寫其中關於鍊錶的解題思路 劍指offer18題 需要兩個指標,pre儲存刪除節點的前驅指標,cur儲存要刪除節點的指標 public listnode deletenode listnode head,int val if cur null return dumm...

劍指offer刷題之 鍊錶

題目描述 在乙個排序的鍊錶中,存在重複的結點,請刪除該鍊錶中重複的結點,重複的結點不保留,返回煉表頭指標。例如,鍊錶1 2 3 3 4 4 5 處理後為 1 2 5 思路 非遞迴 1 首先新增乙個頭節點,以方便碰到第乙個,第二個節點就相同的情況 2 設定 p1 p2 指標,p1指標指向當前確定不重複...