leetcode 24
leetcode题目链接
代码随想录文章讲解
代码随想录视频讲解
两两交换链表中的节点
方法一:虚拟头节点
重点
- 还是需要定义虚拟头节点:因为指针一定要指向要反转的两个节点的前一个节点
- 节点数为奇数:
current
的next
的next
为null
,遍历结束 - 节点数为偶数:
current
的next
的为null
,遍历结束(适用于节点数是0) while
循环中的条件为:current
的next
以及current
的next
的next
均不为空,再进入循环。但注意,判断的时候一定要先判断current
的next
,否则容易出现空指针异常- 逻辑如下:
- 先定义临时节点
temp
保存current
的next
(节点1) temp1
保存节点3- 让
current
的next
指向current
的next
的next
- 让
current
的next
的next
指向temp
- 让
temp
的next
指向temp1
- 将
current
向后移动2个:current
的next
的next
为现在的current
- 先定义临时节点
完整python版答案
# 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: ListNode) -> ListNode:
dummy_head = ListNode(next=head)
current = dummy_head
# 必须有cur的下一个和下下个才能交换,否则说明已经交换结束了
while current.next and current.next.next:
temp = current.next # 防止节点修改
temp1 = current.next.next.next
current.next = current.next.next
current.next.next = temp
temp.next = temp1
current = current.next.next
return dummy_head.next
方法二:递归
完整python版答案
# 递归版本
# 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]:
if head is None or head.next is None:
return head
# 待翻转的两个node分别是pre和cur
pre = head
cur = head.next
next = head.next.next
cur.next = pre # 交换
pre.next = self.swapPairs(next) # 将以next为head的后续链表两两交换
return cur
leetcode 19
leetcode题目链接
代码随想录文章讲解
代码随想录视频讲解
删除链表倒数第N个节点
方法:
重点
- 同样的,操作指针一定指向要删除的节点的上一个节点,即倒数第
n+1
个节点 - 同样添加虚拟头节点
- 有两个指针,分别是快慢指针,让快指针先多走
n+1
步,之后快慢指针一起走,知道快指针指向了最后的null
,此时慢指针应该正好在倒数第n+1
个节点 - 逻辑如下:
- 快慢指针都先指向虚拟头节点
- 快指针先向前移动
n+1
步 - 快慢指针同时向后移动,直到快指针移动到
null
时 - 将慢指针的
next
指向慢指针的next
的next
完整python版答案
# 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: ListNode, n: int) -> ListNode:
# 创建一个虚拟节点,并将其下一个指针设置为链表的头部
dummy_head = ListNode(0, head)
# 创建两个指针,慢指针和快指针,并将它们初始化为虚拟节点
slow = fast = dummy_head
# 快指针比慢指针快 n+1 步
for i in range(n+1):
fast = fast.next
# 移动两个指针,直到快速指针到达链表的末尾
while fast:
slow = slow.next
fast = fast.next
# 通过更新第 (n-1) 个节点的 next 指针删除第 n 个节点
slow.next = slow.next.next
return dummy_head.next
leetcode 160
leetcode题目链接
代码随想录文章讲解
代码随想录视频讲解
环形链表
方法一:快慢指针法
重点
- 快指针一次走2步,慢指针一次走1步,当他们相遇的时候就确定是环形链表
- 为什么快慢指针一定会相遇?
- 因为快指针相对于慢指针是每次快了1步
- 如何找到环的入口?
- 设起始位置到环的入口处长度为X,设环的入口处到相遇点的距离为Y,设相遇点再回到环的入口处的距离为Z。这3个变量可以写出一个等式,桥梁即快指针走2个节点,慢指针1个节点
slow = x + y
fast = x + y + n ( y + z )
(n
即相遇时快指针已经在环里转了n
圈了)- 等式:
2(x+y)=x+y+n(y+z)
x+y=n(y+z)
x=n(y+z)-y # n>=1
# 此时进行变形,使得n圈变成n-1圈,为了把y变成正的
# x=(n-1)(y+2)+z
# 在n=1的情况下,x=z
# 即慢指针在起始处,快指针在相遇处,他们一起移动的话一定会在环的入口处相遇
完整python版答案
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# If there is a cycle, the slow and fast pointers will eventually meet
if slow == fast:
# Move one of the pointers back to the start of the list
slow = head
while slow != fast:
slow = slow.next
fast = fast.next
return slow
# If there is no cycle, return None
return None
方法二:集合法
重点
完整python版答案
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
visited = set()
while head:
if head in visited:
return head
visited.add(head)
head = head.next
return None