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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

算法(10)-leetcode-explore-learn-数据结构-链表双指针技巧

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

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

  • 1.概述
  • 2.例題
    • 2.1 環形鏈表判斷
    • 2.2 環形鏈表2
    • 2.3 相交鏈表
    • 2.4 刪除鏈表的倒數第N個節點
  • 3.小結

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

所有例題的編程語言為python

1.概述

兩種使用雙指針的技巧
(1).兩個指針從不同的位置出發:一個從始端開始,另一個從末端開始
(2).兩個指針以不同的速度移動:一個指針快一些,另一個指針慢一些
針對單鏈表:只能從一個方向遍歷,所以單鏈表中的雙指針技巧為第二種情形。

2.例題

2.1 環形鏈表判斷

給定一個鏈表,判斷鏈表中是否有環。

為了表示給定鏈表中的環,我們使用整數 pos 來表示鏈表尾連接到鏈表中的位置(索引從 0 開始)。 如果 pos 是 -1,則在該鏈表中沒有環。

解法1: hash表。

# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = Noneclass Solution(object):def hasCycle(self, head):""":type head: ListNode:rtype: bool"""if head==None or head.next==None:return Falsehas=[]r1=headwhile(r1):if r1 in has:return Truehas.append(r1)r1=r1.nextreturn False

解法2: 快慢指針
快指針一次跑兩步,慢指針一次跑一步。如果無環,則快指針將先到達尾部;如果有環,快慢指針終將相遇。

class Solution(object):def hasCycle(self, head):""":type head: ListNode:rtype: bool"""if head==None or head.next==None:return Falseslow=headfast=head.nextwhile(slow!=fast):if fast==None or fast.next==None:return Falsefast=fast.next.nextslow=slow.nextreturn True

2.2 環形鏈表2

給定一個鏈表,返回鏈表開始入環的第一個節點。 如果鏈表無環,則返回 null。

為了表示給定鏈表中的環,我們使用整數 pos 來表示鏈表尾連接到鏈表中的位置(索引從 0 開始)。 如果 pos 是 -1,則在該鏈表中沒有環。

說明:不允許修改給定的鏈表。

解法1: hash表
思路在上題的基礎上,如果有環,返回入環節點,而非true即可

class Solution(object):def detectCycle(self, head):""":type head: ListNode:rtype: ListNode"""if head==None or head.next==None:return Nonehas=[]r1=headwhile(r1):if r1 in has:return r1has.append(r1)r1=r1.nextreturn None

解法2:快慢指針,弗洛伊德算法
快慢指針會相遇,但不是在環的起點處,需要再遍歷一次才能找到環的起點

class Solution(object):def detectCycle(self, head):""":type head: ListNode:rtype: ListNode"""if head==None or head.next==None:return Noneslow=headfast=head.nextwhile(slow!=fast):if fast==None or fast.next==None:return Nonefast=fast.next.nextslow=slow.nextfast=headslow=slow.nextwhile(slow!=fast):slow=slow.nextfast=fast.nextreturn slow

2.3 相交鏈表

判斷兩相交鏈表相交時的起始節點。–先判斷兩個鏈表相交否
創建兩個指針 pa和 pb,分別初始化為鏈表 A 和 B 的頭結點。然后讓它們向后逐結點遍歷。
當 pa 到達鏈表的尾部時,將它重定位到鏈表 B 的頭結點 (你沒看錯,就是鏈表 B); 類似的,當 pb 到達鏈表的尾部時,將它重定位到鏈表 A 的頭結點。
若在某一時刻 pa 和 pb 相遇,則 pa/pb 為相交結點。
(速度一樣,走過相同的路徑,如果有交點,必定會在交點處相遇)

# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = Noneclass Solution(object):def getIntersectionNode(self, headA, headB):""":type head1, head1: ListNode:rtype: ListNode"""# 先判斷有沒有交點if headA==None or headB==None:return Nonepa=headApb=headBwhile(pa.next):pa=pa.nextwhile(pb.next):pb=pb.nextif pb!=pa:return Noneelse:pa=headApb=headBwhile(pa!=pb):if pa.next:pa=pa.nextelse:pa=headBif pb.next:pb=pb.nextelse:pb=headAreturn pa

2.4 刪除鏈表的倒數第N個節點

首先我們將添加一個啞結點作為輔助,該結點位于列表頭部。啞結點用來簡化某些極端情況,例如列表中只含有一個結點,或需要刪除列表的頭部。
雙指針解題

# Definition for singly-linked list. class ListNode(object):def __init__(self, x):self.val = xself.next = Noneclass Solution(object):def removeNthFromEnd(self, head, n):""":type head: ListNode:type n: int:rtype: ListNode"""dummy=ListNode(0)dummy.next=headfirst,second=dummy,dummy#irst.next,second.next=head,headfor i in range(n):first=first.nextwhile(first.next!=None):second=second.nextfirst=first.nextsecond.next=second.next.nextreturn dummy.next

3.小結

注意事項:
1.在調用 next 字段之前,始終檢查節點是否為空。
獲取空節點的下一個節點將導致空指針錯誤。例如,在我們運行 fast = fast.next.next 之前,需要檢查 fast 和 fast.next 不為空。

總結

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

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