LeetCode鍊錶簡單題

2022-03-17 07:51:04 字數 4520 閱讀 7557

一、21合併兩個有序鍊錶

**如下:

class

solution:

def mergetwolists(self, l1: listnode, l2: listnode) ->listnode:

#首先對特殊情況進行處理,l1 l2 為空的時候

ifnot (l1 and l2) : #

這個表示式的意思只要l1 l2 不全為真就符合條件

return l1 or

l2

elif l1.val > l2.val: #

判斷哪乙個值比較小,然後保留哪一項

l2.next = self.mergetwolists(l2.next,l1) #

遞迴思想的精髓

return l2 #

返回小的這一項

else

: l1.next =self.mergetwolists(l1.next,l2)

returnl1#

注意:l1 l2 表示的不是整個鍊錶,而只是每乙個結點

#這個是別人給的題解,大佬寫的很好。

class

solution:

def mergetwolists(self, l1: listnode, l2: listnode) ->listnode:

if l1 and l2: #

判斷l1 是否為空

if l1.val > l2.val : l1,l2 = l2,l1 #

始終讓l1 為小值

l1.next = self.mergetwolists(l1.next,l2) #

遞迴的精髓

return l1 or l2

二、83刪除鍊錶重複的元素

class

solution:

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

ifnot head :return head #

首先判斷鍊錶是否為空,若為空直接返回

head1 =head

l = #

定義乙個空的列表 用來存放已經遍歷過的鍊錶結點

將遍歷過的鍊錶結點新增進入列表中

while head and head.next: #

當前結點和下乙個結點都為真繼續

if head.next.val in l : #

若下一節點的值已經在列表中

head.next = head.next.next #

進行刪除操作,跳過下一節點,直接指向下下結點

else:#

將結點新增進入列表

head = head.next #

進行遍歷

return head1 #

返回刪除重複元素後的鍊錶

三、141環形鍊錶

#

解題思路;利用快慢指標,乙個指標快,另乙個慢,若煉表有環,則總有相遇的時候

class

solution(object):

defhascycle(self, head):

""":type head: listnode

:rtype: bool

"""if

not head or

not head.next : #

若煉表中沒有元素,或者只有乙個元素,則不可能成環

return

false

fast =head

slow =head

while

fast:

if fast.next == none: #

若快指標可以走到頭,則肯定沒有環

return

false

else

: fast = fast.next.next #

快指標每次走兩步

slow = slow.next #

慢指標走一步

if slow == fast: #

當快慢指標相遇

return

true

return false #

當快指標為none時退出

四、160相交鍊錶

class

solution(object):

defgetintersectionnode(self, heada, headb):

""":type head1, head1: listnode

:rtype: listnode

"""if

not heada or

not headb: #

若兩個鍊錶中其中乙個鍊錶為空則不可能相交

return

none

length1 = 0 #

用來接收heada鍊錶的長度

length2 =0

h1 =heada

h2 =headb

while h1: #

遍歷煉表算出兩個鍊錶的長度

length1 += 1h1 =h1.next

while

h2: length2 += 1h2 =h2.next

if length1 > length2: #

讓比較長的鍊錶先走

for i in range(length1 -length2):

heada =heada.next

elif length1 for i in range(length2 -length1):

headb =headb.next

while heada: #

現在兩個指標處於同一位置

if heada == headb: #

判斷是否為同一節點

return

heada

heada =heada.next

headb =headb.next

return none

五、203移除鍊錶元素

class

solution(object):

defremoveelements(self, head, val):

""":type head: listnode

:type val: int

:rtype: listnode

"""if

not head:return head #

判斷是否為空鍊錶

head1 = head #

用head1返回新的鍊錶

while head: #

找到第乙個不需要刪除的鍊錶

if head.val == val: #

需要刪除時,將head1等於head的下一節點

head1 =head.next

head =head.next

else

:

break

while head and head.next: #

進行鍊錶的遍歷,進行刪除所給的元素

if head.next.val ==val:

head.next =head.next.next

else

: head =head.next

return head1 #

返回新的鍊錶

六、234回文鍊錶

#

用快慢指標的方法找到鍊錶的中間節點,然後將後半段鍊錶進行反轉,在判斷

class

solution:

def ispalindrome(self, head: listnode) ->bool:

ifnot head or

not head.next : #

若煉表為空或為乙個元素,則鍊錶為回文鍊錶

return

true

fast =head

slow = head #

快慢指標 快指標走兩步,慢指標走一步

while fast and

fast.next:

fast =fast.next.next

slow =slow.next

mid = slow #

此時slow為中點指標

pre =none

#將後半段煉表表反轉

while

slow:

slow.next,pre,slow =pre,slow,slow.next

#不可以分開寫成三個語句,python特有,在c語言中這樣寫是錯誤的

#他們這三個語句的執行是相同,沒有先後之分。

#此時後半段列表的頭指標為pre

while head !=mid:

if head.val !=pre.val:

return

false

pre =pre.next

head =head.next

return true

leetcode 鍊錶(簡單)

鍊錶這部分的題比較簡單 21合併兩個鍊錶 類似於歸併排序,略 83刪除鍊錶中重複元素 判斷下乙個是否於當前相同,相同則跳過 listnode deleteduplicates listnode head else return head 環形鍊錶 兩種解法 是使用快慢指標,但我做的時候死迴圈了 是使...

leetcode鍊錶題心得

給定乙個排序鍊錶,刪除所有重複的元素,使得每個元素只出現一次。示例 1 輸入 1 1 2輸出 1 2示例 2 輸入 1 1 2 3 3輸出 1 2 3思路一 直接方法 definition for singly linked list.class listnode def init self,x s...

LeetCode題 旋轉鍊錶

原題 給定乙個鍊錶,旋轉鍊錶,將鍊錶每個節點向右移動 k 個位置,其中 k 是非負數。示例 1 輸入 1 2 3 4 5 null,k 2 輸出 4 5 1 2 3 null 解釋 向右旋轉 1 步 5 1 2 3 4 null 向右旋轉 2 步 4 5 1 2 3 null 示例 2 輸入 0 1...