代码随想录第四天链表part02|24. 两两交换链表中的节点、19.删除链表的倒数第N个节点、面试题 02.07. 链表相交、142.环形链表II
24. 两两交换链表中的节点【面试易考题】
思路
:
使用虚拟头结点的方法,改变节点指向的顺序非常重要,
奇数偶数个节点情况考虑:
curr = dummyhead
①奇数:curr.next为空,结束
②偶数:curr.next.next为空,结束
伪代码
:
dummyhead.next = head
curr = dummyhead # 要在两个节点前面才能操作那两个节点
while curr.next != NULL and curr.next.next != NULL:
temp = curr.next # 记录节点1
temp1 = curr.next.next.next # 记录节点3
curr.next = curr.next.next # 节点dummy指向节点2
curr.next.next = temp # 节点2指向节点1
temp.next= temp1 # 节点1指向节点3
curr = curr.next.next # curr向后移动两位
return dummyhead.next
python代码
:
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummyhead = ListNode()
dummyhead.next = head
curr = dummyhead
while curr.next and curr.next.next:
temp = curr.next
temp1 = curr.next.next.next
curr.next = curr.next.next
curr.next.next = temp # 不是curr.next.next.next = temp 因为curr.next已经改了
temp.next = temp1
curr = curr.next.next
return dummyhead.next
// 第二种方法 不用虚拟头结点
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
return head
p = head
new_head = p.next
q = p.next
temp = q.next
q.next = p
while temp and temp.next:
p.next = temp.next
p = temp
q = p.next
temp = q.next
q.next = p
p.next = temp
return new_head
19.删除链表的倒数第N个节点
思路
:
①使用虚拟头节点,统一操作
②双指针(快慢指针)的经典应用(找倒数第n个节点):
slow指向dummyhead;fast指向第n+1个节点(为什么是n+1呢,因为只有这样同时移动的时候slow才能指向删除节点的上一个节点(方便做删除操作))
fast和slow同时向后移动,slow指向NULL;fast指向第n-1个节点
伪代码
:
n++ //安全起见
fast = dummyhead
slow = dummyhead
while (n--- && fast != NULL)
fast = fast->next
while(fast != NULL)
fast = fast->next
slow = slow->next
slow->next = slow->next->next
return dummyhead.next
python代码
:
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dummyhead = ListNode(0,head)
fast = dummyhead
slow = dummyhead
n = n + 1
while n:
fast = fast.next
n-=1
while fast:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dummyhead.next
面试题 02.07. 链表相交
待补充
142.环形链表II
1)快慢指针法
思路
使用双指针的思路判断链表是否有环:因为快指针与慢指针的相对速度是一个节点,即进入环后快指针每次距离慢指针都减少一个节点,所以它们一定会相遇,而不会出现快指针跳过慢指针的情况,并且在慢指针进入环的第一圈就会相遇。
如下图所示,进行详细分析
慢指针走了x+y个节点
快指针走了x+y+n(y+z)个节点,n是快指针转的圈数,n≥1
2(x+y) = x+y+n(y+z)
x+y = n(y+z)
x=n(y+z)-y
x=(n-1)(y+z) + z
若n=1,x=z,在环的入口处相遇
可想而知,无论n为多少,x都等于z,所以判断存在环之后,要寻找环形的入口节点,定义index1和index2,让它们同时移动就可以了(另一个快慢指针)
伪代码
fast = head
slow = head
while(fast != NULL && fast->next != NULL)
fast = fast->next->next
slow = slow->next
if(fast == slow)
index1 = fast
index2 = head
while(index1 != index2)
index1 = index1->next
index2 = index2->next
return indx1
return NULL
python代码
注意while中的边界条件需要仔细考虑,while curr而不是while curr.next,因为最后一个是空指针,temp = curr.next
class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
fast = head
slow = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if fast == slow:
index1 = fast
index2 = head
while index1 != index2:
index1 = index1.next
index2 = index2.next
return index1
return None
时间复杂度:O(n)
空间复杂度:O(1)
2)集合法
思路
通过集合这个数据类型可以更简便得到答案
python代码
class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
visited = set()
while head:
if head in visited:
return head
visited.add(head)
head = head.next
return None
在具体写代码时,有很多细节需要额外关注