Leetcode 快慢指標與鍊錶

2021-09-11 08:30:16 字數 4308 閱讀 7009

1.給定乙個鍊錶,判斷鍊錶中是否有環。

解釋:鍊錶中有乙個環,其尾部連線到第二個節點。

ps:從例子中根本看不出來好嘛。。。。。。自己理解就ok

經典思想,快慢指標,如果有環存在,快指標總會追上慢指標

python**

# definition for singly-linked list.

# class listnode(object):

# def __init__(self, x):

# self.val = x

# self.next = none

class

solution

(object):

defhascycle

(self, head)

:"""

:type head: listnode

:rtype: bool

"""ifnot head:

return

false

first=head

second=head

while

true

:if first.

next

isnone

or first.

next

.next

isnone

:return

false

first=first.

next

.next

# 快指標,二倍速

second=second.

next

# 慢指標 ,一倍速

if first==second:

return

true

c++**

/**

* definition for singly-linked list.

* struct listnode

* };

*/class solution

return0;

}};

給定乙個鍊錶,返回鍊錶開始入環的第乙個節點。 如果鍊錶無環,則返回 null。

說明:不允許修改給定的鍊錶。

引理證明:快指標追上慢指標的時候,慢指標距離環形鍊錶初始點的長度等於原始煉表頭距離環形鍊錶初始點的長度。

設原始鍊錶中 環形部分長度為n,非環型部分長度為m。(不妨設n>m,其他形況相同,不一一枚舉了)

則慢針入環時,快針剛好領先慢針m個,則可看成,慢針領先快針(n-m)個。即,在(n-m)步後,快指標追上慢指標,此時慢指標距離入口處剛好(n-(n-m))=m個距離

python**如下

# definition for singly-linked list.

# class listnode(object):

# def __init__(self, x):

# self.val = x

# self.next = none

class

solution

(object):

defdetectcycle

(self, head)

:"""

:type head: listnode

:rtype: listnode

"""# 1,快慢指標判斷是否有環

fast = head

slow = head

while fast !=

none

and fast.

next

!=none

: fast = fast.

next

.next

slow = slow.

next

if fast == slow:

break

if fast ==

none

or fast.

next

==none

:return

none

# 2, 慢指標繼續 快指標從頭開始

# 按照定理的推展

while head != slow:

head = head.

next

slow = slow.

next

return head

c++**

待續編寫乙個程式,找到兩個單鏈表相交的起始節點。

將a鏈的結尾,連上b的開頭,則題和題目二同理

python**

# definition for singly-linked list.

# class listnode(object):

# def __init__(self, x):

# self.val = x

# self.next = none

class

solution

(object):

defgetintersectionnode

(self, heada, headb)

:"""

:type head1, head1: listnode

:rtype: listnode

"""## 將鍊錶尾部 連到另乙個鍊錶的頭部,就如同上題了

ifnot heada or

not headb:

return

none

fast = heada

slow = heada

head = heada

while heada.

next

: heada = heada.

next

heada.

next

= headb

## 判斷是否有環

while fast and fast.

next

: fast = fast.

next

.next

slow = slow.

next

if fast == slow:

break

if fast ==

none

or fast.

next

==none

: heada.

next

=none

## 題目要求返回後鍊錶結構和之前一樣

return

none

## 找環起點

while

not head == slow:

head = head.

next

slow = slow.

next

heada.

next

=none

return head

尋找鍊錶中倒數第k個節點

先讓快指標走k-1個節點,然後慢指標出發,快指標到盡頭了,慢指標所指就是倒數第k個節點

class

solution

:def

findkthtotail

(self, head, k)

:# write code here

if head==

none

or k<=0:

return

none

#設定兩個指標,p2指標先走(k-1)步,然後再一起走,當p2為最後乙個時,p1就為倒數第k個 數

p2=head

p1=head

#p2先走,走k-1步,如果k大於鍊錶長度則返回 空,否則的話繼續走

while k>1:

if p2.

next

!=none

: p2=p2.

next

k-=1else

:return

none

#兩個指標一起 走,一直到p2為最後乙個,p1即為所求

while p2.

next

!=none

: p1=p1.

next

p2=p2.

next

return p1

鍊錶與快慢指標

之前刷劍指offer遇到尋找鍊錶環的入口節點,需要使用到快慢指標,然後題一變,發現自己總是不能立馬聯想起來。總結一下快慢指標法在鍊錶中的一些常見的用處。leetcode 141 快指標每次走2步,慢指標每次走1步,如果鍊錶中有環,則兩個指標就會相遇。public boolean hascycle l...

LeetCode 鍊錶 快慢指標問題

前言 這是關於leetcode上關於雙指標問題的乙個小結 1.環形鍊錶 141.給定乙個鍊錶,判斷鍊錶中是否有環。為了表示給定鍊錶中的環,我們使用整數 pos 來表示鍊錶尾連線到鍊錶中的位置 索引從 0 開始 如果 pos 是 1,則在該鍊錶中沒有環。示例 1 輸入 head 3,2,0,4 pos...

鍊錶快慢指標

public listnode removenthfromend listnode head,int n 為了找到要刪除的節點的前乙個節點,所以此處讓fast.next null while fast.next null 此時head為倒數第n個節點的前乙個節點。slow.next slow.nex...