今日任务
- 两两交换链表中的节点
- 19.删除链表的倒数第N个节点
- 面试题 02.07. 链表相交
- 142.环形链表II
- 总结
24. 两两交换链表中的节点
用虚拟头结点,这样会方便很多。 本题链表操作就比较复杂了,建议大家先看视频,视频里我讲解了注意事项,为什么需要temp保存临时节点。
题目链接/文章讲解/视频讲解:
https://programmercarl.com/0024.%E4%B8%A4%E4%B8%A4%E4%BA%A4%E6%8D%A2%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.html
思路梳理:
- 虚拟头节点:让每一个节点都可以用cur.next指向(相当于索引)
- 交换的前提:.next和.next.next都存在
- 初始化cur:虚拟头节点
- 要改变三个next的位置,就要设置两个虚拟变量,分别是首位和末位
# 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]:
dummy_head = ListNode(next=head)
current = dummy_head
while current.next and current.next.next:
temp1 = current.next
temp2 = current.next.next.next
current.next = current.next.next
current.next.next = temp1
temp1.next = temp2
current = current.next.next #注意是两两交换,因此要隔一个更新
return dummy_head.next #注意输出的不是head
19.删除链表的倒数第N个节点
双指针的操作,要注意,删除第N个节点,那么我们当前遍历的指针一定要指向 第N个节点的前一个节点,建议先看视频。
题目链接/文章讲解/视频讲解:https://programmercarl.com/0019.%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E5%80%92%E6%95%B0%E7%AC%ACN%E4%B8%AA%E8%8A%82%E7%82%B9.html
- 注意!是双指针,但是也要虚拟头节点
- 链表只有一个节点
- 整体思路
- 虚拟头节点;fast和slow都初始化为虚拟头节点(因为链表有可能只有一个节点,最后第一个就是当前的,fast直接就移动到了null)
- 双指针:fast首先移动n+1个位置(因为有虚拟头节点),fast再和slow一起移动,直到fast移动到了链表的最后(while fast),这个时候slow就移动到了倒数第N个节点的前一个节点
- 删除操作:slow.next = slow.next.next
- 代码
# 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]:
dummy_head = ListNode(next=head)
fast,slow = dummy_head,dummy_head
for _ in range(n+1): # 因为有虚拟头节点,所以要走n+1步
fast = fast.next
while fast: # 只要判断fast存在
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dummy_head.next # 不能直接return head
面试题 02.07. 链表相交
本题没有视频讲解,大家注意 数值相同,不代表指针相同。
题目链接/文章讲解:https://programmercarl.com/%E9%9D%A2%E8%AF%95%E9%A2%9802.07.%E9%93%BE%E8%A1%A8%E7%9B%B8%E4%BA%A4.html
-
发现又不懂指针,头节点什么的,不知道是不是用python实现的原因
-
实现思路
- 通过移动较长的链表,使两链表长度相等
- 将两个头向前移动,直到它们相交
-
代码包含(复用的实现方式)
- 求链表的长度
- 移动头节点的位置
- 寻找相交的位置
# 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:
dis = self.getLength(headA) - self.getLength(headB)
if dis > 0 :
headA = self.moveForwad(headA, dis)
else:
headB = self.moveForwad(headB, abs(dis))
while headA:
if headA == headB:
return headA
headA = headA.next
headB = headB.next
return None
def getLength(self, head):
length = 0
while head:
length += 1
head = head.next
return length
def moveForwad(self,head,steps):
while steps > 0: # 移动steps步
head = head.next
steps -= 1
return head
142.环形链表II
算是链表比较有难度的题目,需要多花点时间理解 确定环和找环入口,建议先看视频。
题目链接/文章讲解/视频讲解:https://programmercarl.com/0142.%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8II.html
- 两个部分
- 判断是否有环:双指针是否会相遇
- 快慢指针:初始位置均为头节点,一个速度为两个节点,一个速度为一个节点
- 判断环的入口:双指针相遇
- 环内外指针:环外指针初始位置为头节点,环内指针初始位置为快慢指针相遇的节点,速度均为一个节点
- 根据追及计算,快慢指针相遇的位置到环的出口(即环的入口)=链表的头节点到环的入口
- 判断是否有环:双指针是否会相遇
- 整体思路:可以看做一个追及问题
- 快指针的速度 = 2 * 慢指针的速度
- 快指针的速度 - 慢指针的速度 = 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]:
fast, slow = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
# 设置两个新的指针,一个初始位置为头节点,另一个初始位置为环内相遇的节点
# 这两个指针以相同的速度移动,相遇的节点即为入口
out_index = head
in_index = fast
while out_index != in_index :
out_index = out_index.next
in_index = in_index.next
return out_index
return None