劍指offer 鍊錶題目集合

2021-09-26 07:10:30 字數 4176 閱讀 5389

從尾到頭列印鍊錶

class solution:

# 返回從尾部到頭部的列表值序列,例如[1,2,3]

def printlistfromtailtohead(self, listnode):

rs =

if not listnode:return rs

while listnode:

listnode = listnode.next

return rs[::-1]

刪除鍊錶中重複節點

# -*- coding:utf-8 -*-

class listnode:

def __init__(self, x):

self.val = x

self.next = none

class solution:

def deleteduplication(self, phead):

# write code here

if not phead:return phead

dummy = listnode(-1)

dummy.next = phead

p = dummy

while p.next:

# 原來錯誤是while dummy.next:

# dummy在while中都沒有發生改變,起不到迴圈結束的作用

q = p.next

while (q and p.next.val == q.val):

q = q.next

# 如果不存在重複,長度==1

if p.next.next == q:

p = p.next

# 如果存在重複,長度》1

else: p.next = q

return dummy.next

反轉鍊錶

class solution:

def reverselist(self, phead):

# 反轉鍊錶需要三個listnode

pre = none

cur = phead

while cur:

tail = cur.next

cur.next = pre

pre = cur

cur = tail

# return cur # cur目前指的是空

return pre

兩個鍊錶出現的第乙個公共節點(考)

class solution:

def findfirstcommonnode(self, phead1, phead2):

if not phead1:return phead1

if not phead2:return phead2

len1 = self.getlength(phead1)

len2 = self.getlength(phead2)

diff = abs(len1-len2)

p1 = phead1

p2 = phead2

if len1>len2:

while diff>0:

diff -= 1

p1= p1.next

if len10:

diff -= 1

p2 = p2.next

# print(p1.val)

# print(p2.val)

while p1 != p2:

p1 = p1.next

p2 = p2.next

return p1

def getlength(self,head):

cnt = 0

cur = head

while cur:

cnt+=1

cur = cur.next

return cnt

合併兩個鍊錶

class listnode:

def __init__(self, x):

self.val = x

self.next = none

class solution:

# 返回合併後列表

def merge(self, phead1, phead2):

# write code here

p1,p2 = none,none

rs = none

head = rs

while phead1 and phead2:

if phead1.val < phead2.val:

rs.next = phead1

phead1 = phead1.next

else:

rs.next = phead2

phead2 = phead2

rs = rs.next

while phead1:

rs.next = phead1

while phead2:

rs.next = phead2

return head

複雜鍊錶的複製

class solution:

# 返回 randomlistnode

def clone(self, phead):

if not phead:return none

p = phead

# 完成節點複製

while p:

node = randomlistnode(p.label)

tail = p.next

p.next = node

node.next=tail

p = tail

# 複製隨機指標

q = phead

while q:

if q.random:

q.next.random = q.random.next

q = q.next

# 開始連線複製的節點

clonode = phead

phead = phead.next

while clonode.next:

node = clonode.next

clonode.next = node.next

clonode = node

return phead

鍊錶中倒數第k個節點 

class solution:

def findkthtotail(self, head, k):

# write code here

if not head:return head

p = head

cnt = 0

while p:

cnt += 1

p = p.next

if k>cnt:return

n = cnt-k+1

dummy = listnode(-1)

dummy.next = head

q = dummy

cnt = 0

while q.next:

cnt += 1

if cnt==n:

return q.next.val

q = q.next

鍊錶中環入口的位置

class solution:

def entrynodeofloop(self, phead):

p1 = phead #slow

p2 = phead #fast

meet = phead

flag = 0

while p2 and p2.next:

p1 = p1.next

p2 = p2.next.next

if p1 == p2:

flag = 1

meet = p1

break

if not flag:

return none

start = phead

while meet and start:

if meet == start:

return start

meet = meet.next

start = start.next

return none

《劍指offer 之鍊錶題目

這幾天在看 劍指offer 寫的很好,跟july的各有千秋,更加注重歸納總結,筆試面試臨時抱佛腳,決定要啃下這其中的46道題目。ok,把每道題目自己實現,相同的歸成一類。這次把所有的與list相關的題目列出來。思路 從尾到頭列印list的話,考慮用stack先把各節點儲存起來,遍歷完以後再將stac...

劍指Offer題目1518 反轉鍊錶

題目1518 反轉鍊錶 時間限制 1 秒 記憶體限制 128 兆 特殊判題 否 提交 3300 解決 1207 題目描述 輸入乙個鍊錶,反轉鍊錶後,輸出鍊錶的所有元素。hint 請務必使用鍊錶 輸入 輸入可能包含多個測試樣例,輸入以eof結束。對於每個測試案例,輸入的第一行為乙個整數n 0 n 10...

劍指offer 鍊錶

單向鍊錶的結構定義 typedef int datatype struct listnode 問題1 往鍊錶的末尾新增乙個結點 給定頭結點,往末尾插入乙個結點 void insertnode listnode head,datatype key listnode p head while p nex...