24. 两两交换链表中的节点
- 两两交换链表中的节点难点在于搞清楚要先让那两个节点进行交换,以及交换的顺序是怎么样的?
- 以题目为例: 我们的操作指针指向2,需要先保存操作指针前一个节点,以及操作指针后一个节点,也就是说需要用temp保存节点1和节点3
- 让指针2 指向 指针1, 再让 指针1 指向指针3
- 原链表: 1->2->3->4
- 目前: 2->1->3->4
- 我们已经换掉了前两个节点,但是3和4还没有换
- 当完成上面交换操作之后,让操作指针移动到下个要操作的节点前一位,这里也就是移动到节点1
- 使用while循环重复上述操作即可;while循环的遍历条件需注意:
- current.next != null && current.next.next != null
- 这两个顺序是有差别的,一定要让current,next写在前面,如果让current.next.next在前面先进行判断的话很有可能会遇到 空指针而引发报错!
- current.next在前面遇到空的话会利用 逻辑运算符 短路特性 从而直接返回False跳出循环
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
we need let 2 point to 1 first, then let 1 point to 3,
after we doing this, move our pointer to the node 3 before(original linklist)
in order operate the linklist like this,
we should use temp to store the node before current node, and after current node
"""
# create dummy_head
dummy_head = ListNode(next=head)
current = dummy_head
# while loop will condition shoudl be cur->next && cur->next->next == None
# we should let current.next write first, otherwise it might appear Error, empty pointer
while(current.next is not None and current.next.next is not None):
# store the node before current pointer(save node 1 address)
temp = current.next
# store the node after current pointer(save node 3 address)
temp1 = current.next.next.next
# let current move to node 2
current.next = current.next.next
# start swap
# let node 2 point to node 1
current.next.next = temp
# let node 1 point to node 3
temp.next = temp1
# move our current pointer to node 3 position, to swap 3 and 4
current = current.next.next
# return our head
return dummy_head.next
19. 删除链表的倒数第N个节点
- 首先我们要达到链表的末尾,其次要能获取到要移除节点的前一个节点(链表中不能删除元素,只能通过修改要删除节点的前一个节点地址指向下下个节点,从而达到删除的目的)意味着我们要使用双指针(快慢指针); 因为两个指针要移动到的地方不一样
- 这里我们需要让fast指针移动 n+1步数,如果只是移动n步的话,等下我们fast和slow指针一起移动slow指针指向的位置不是要移除节点的前一个,而是指向要移除的节点,所以这里fast要先走n+1步
- 判断fast指针有没有达到链表的尾部,让fast和slow指针一起移动
- 让slow指针指向下下个节点达成删除节点目的
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
use two pointer method
let fast pointer move nth+1 step ahead, and then fast and slow pointer move togther
"""
# create dummy_head
dummy_head = ListNode(next=head)
# create two pointer, fast and slow
fast = dummy_head
slow = dummy_head
# let fast move nth+1 step ahead
for _ in range(n):
fast = fast.next
# this is + 1 step,purpose is let slow can reach the node before nth node(remove node)
fast = fast.next
# fast and slow pointer move toghter
while fast:
fast = fast.next
slow = slow.next
# slow point to the node before nth node
slow.next = slow.next.next
return dummy_head.next
142. 环形链表 II(双指针)
-
判断环是否存在:
- 使用快慢指针判断链表是否有环
- 如果没有环是个直线,快慢指针永远不可能相遇
-
如果快慢指针相遇了,说明有环
-
快指针每次走两个节点,慢指针走一个节点; 从起点出发
-
快慢指针一定相遇,在环中快指针其实就是在追慢指针(可以想象成跑步套圈)
-
快指针对于慢指针来说是每次以一个节点速度去靠近慢指针;所以永远不可能跳过慢指针而是两个指针相遇
-
-
环存在,判断入口:(下面为数学证明,可以跳过;结论为一个指针在起点一个指针在fast和slow指针相交点,两者一起移动最后会在环的入口相遇)
-
从起点到入口处设为x
2. 从入口位置到相遇位置设为y
3. 从相遇位置到入口位置设为z
1. slow pointer = x+y ; 速度
2. fast pointer = x+y+n(y+z); n代表快慢指针相遇之前快指针在环里走了几圈的圈数 ; 速度
3. 通过 distance / velocity * time来列出下面等式,因为时间相同路程相同所以速度可以列为一样
4. 2(x+y) = x+y+n(y+z)
1. x+y = n(y+z)
2. x = n(y+z)-y
3. n >= 1因为快指针肯定要最少跑了一圈才能追上慢指针
4. x = ny+nz-y
5. x = (n-1)y+nz
6. x = (n-1)y+(n-1)z+z; 这里后面的(n-1)z+z其实就是nz只不过是拆开的
7. x = (n-1)(y+z)+z
8. 当n等于1的时候;快指针转了一圈和慢指针相遇了
9. x = z
-
-
通过上述可知,一个指针在起点一个指针在z点,两者一起移动最后会在环的入口相遇
从相遇到地方定义一个指针index1;起点定义一个指针index2;让两者用相同速度移动,最后相遇到点就是环的入口处
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
数学推理
1.slow pointer = x+y ; 速度
2. fast pointer = x+y+n(y+z); n代表快慢指针相遇之前快指针在环里走了几圈的圈数 ; 速度
3. 通过 distance / velocity * time来列出下面等式,因为时间相同路程相同所以速度可以列为一样
4. 2(x+y) = x+y+n(y+z)
1. x+y = n(y+z)
2. x = n(y+z)-y
3. n >= 1因为快指针肯定要最少跑了一圈才能追上慢指针
4. x = ny+nz-y
5. x = (n-1)y+nz
6. x = (n-1)y+(n-1)z+z; 这里后面的(n-1)z+z其实就是nz只不过是拆开的
7. x = (n-1)(y+z)+z
8. 当n等于1的时候;快指针转了一圈和慢指针相遇了
9. x = z
5. 通过上述可知,一个指针在起点一个指针在z点,两者一起移动最后会在环的入口相遇
"""
# use two pointer
fast = head
slow = head
# we don't need to check fast.next.next is none or not, if fast.next || fast is none we end loop
while(fast is not None and fast.next is not None):
# we let fast move two step each time, we let fast pointer cheese slow pointer
fast = fast.next.next
slow = slow.next
# fast and slow met,means it has cycle; detemine cycle is exist or not
if(fast == slow):
# record position where those two pointer met; which is position z
index1 = fast
# record linklist start positionl; which is position x
index2 = head
# since we know that index1 and index2 move same speed, at some point x=z; that means index1=index2
while(index1 != index2):
index1 = index1.next
index2 = index2.next
# finished loop, means index1 = index2; that's our first node in our cycle
return index1
# if we didn't return after inside while loop excute, that means we don't have cycle
return None