易错1:若使用虚拟头节点,题目要求返回实际头节点,记得return dummy_head.next。
易错2:涉及链表长度时,注意区间开闭,比如循环的条件是while cur还是while cur.next、是while window_size < n还是while window_size <= n、起始时指针指向dummy_head还是head,等等。
从(我们创建的)虚拟头节点dum_head开始,每次考虑当前节点往后三个节点p1、p2、p3。若当前节点cur后只剩下<=1节点(cur.next为空或cur.next.next为空)则不需再操作,返回。若cur后存在>=2个节点,需要交换p1和p2,令p1指向p3,将p1赋给cur。即使p3为空也不影响这个if代码块,所以不需要单独考虑p1、p2不为空而p3为空的情况。时间O(n)、空间O(1)。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
dum_head = ListNode(next = head)
cur = dum_head
while cur:
if cur.next is None or cur.next.next is None:
return dum_head.next
else:
p1 = cur.next
p2 = p1.next
p3 = p2.next
cur.next = p2
p2.next = p1
p1.next = p3
cur = p1
19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)
思路类似滑动窗口,较直观但不简洁的写法:维护左侧节点left、待删除节点del_node、右侧节点right、指示窗口结尾的节点cur。窗口是[del_node, cur)。首先将del_node置于链表首个节点head,右移cur直到[del_node, cur)包含n个节点。接着同时右移以上四个节点,直到cur为空,此时del_node就是倒数第n个节点。left.next = right。时间O(n)空间O(1)。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dum_head = ListNode(next = head)
win = 0 # window size
left = dum_head
del_node = head
right = head.next
cur = head
while win < n:
cur = cur.next
win += 1
while cur:
left = left.next
del_node = del_node.next
right = right.next
cur = cur.next
left.next = right
return dum_head.next
简化:其实只需要left和cur即可,如下(left是slow,cur是fast
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dum_head = ListNode(next = head)
win = 0 # window size
slow = dum_head
fast = head
while win < n:
fast = fast.next
win += 1
while fast:
slow = slow.next
fast = fast.next
slow.next = slow.next.next
return dum_head.next
面试题 02.07. 链表相交 - 力扣(LeetCode)
需要遍历一次A和B两个链表以获得各自长度,接着将较长链表与较短的对齐,同步移动两个链表的指针直到为空,一旦指针相同则返回该指针。时间O(n+m)空间O(1)。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
if headA is None or headB is None:
return None
#dum_headA = ListNode(next = headA)
#dum_headB = ListNode(next = headB)
curA = headA
curB = headB
lenA = lenB = 0
#左闭右开
while curA:
curA = curA.next
lenA += 1
while curB:
curB = curB.next
lenB += 1
curB = headB
curA = headA
i = 0
if lenA >= lenB:
while i < (lenA - lenB):
curA = curA.next
i += 1
else:
while i < (lenB - lenA):
curB = curB.next
i += 1
while curA:
if curA == curB:
return curA
curA = curA.next
curB = curB.next
快慢指针法+数学上的分析。slow和fast起始时指向head,每次fast移动两步,slow移动一步。如果fast往后移动时会遇到空(如果无环,fast先到链表尾部,判断fast即可),说明无环,返回。若不为空,slow和fast将在环中相遇,而且相遇时slow未走完一个完整的环(证明见下方链接)。相遇后,将它们在环中相遇节点赋给fast,将head赋给slow,再同步移动两指针,它们将在入环节点相遇(证明见下方链接)。时间O(n)空间O(1)。
[数学证明]
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
slow = head
while slow != fast:
slow = slow.next
fast = fast.next
return fast
return None