24. 两两交换链表中的节点
力扣题目链接 (opens new window)
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
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]:
dummy_head = ListNode(next=head) # 这里赋值可以省去下面单独赋值的步骤
# dummy_head.next = head
cur = dummy_head # 初始化cur节点为虚拟头节点
while cur.next and cur.next.next: # 非空即真
temp = cur.next # 暂存交换的第一个节点
temp1 = cur.next.next.next # 暂存交换的第二个节点指针指向的节点
cur.next = cur.next.next # 步骤1
cur.next.next = temp # 步骤2
temp.next = temp1 # 步骤3
cur = cur.next.next # cur移动两位,准备下一轮交换
return dummy_head.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]:
def swap_Pairs(head):
if head == None or head.next == None:
return head
# 获取当前节点的下一个节点
Next = head.next
# 进行递归
newNode = swap_Pairs(Next.next)
# 这里进行交换
Next.next = head
head.next = newNode
return Next
return swap_Pairs(head)
9.删除链表的倒数第N个节点
力扣题目链接 (opens new window)
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
进阶:你能尝试使用一趟扫描实现吗?(什么是扫描实现呢)
# 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)
slow, fast = dummy_head, dummy_head
while n > 0:
fast = fast.next # fast先往前走n步,此时fast和slow之间差n个节点
n -= 1
while fast.next: # 当fast移动到最后一个节点,此时slow的下一个节点即为倒数第n个节点
slow = slow.next # 再同时移动slow和fast,之间的距离不变,
fast = fast.next
slow.next = slow.next.next # 删除操作
return dummy_head.next
面试题 02.07. 链表相交
同:160.链表相交
力扣题目链接 (opens new window)
给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。
- 法一
# 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:
# 计算链表长度
len_A, len_B = 0, 0
cur_A,cur_B = headA, headB
while cur_A:
len_A += 1
cur_A = cur_A.next
while cur_B:
len_B += 1
cur_B = cur_B.next
cur_A, cur_B = headA, headB
if len_B > len_A: # 让cur_A为最长链表
cur_A, cur_B = headB, headA
len_A, len_B = len_B, len_A
gap = len_A - len_B
while gap and cur_A: # 将 cur_A移动到与B等长的链表节点
cur_A = cur_A.next
gap -= 1
while cur_A:
if cur_A == cur_B:
return cur_B
else:
cur_A = cur_A.next
cur_B = cur_B.next
return None
- 法二
# 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
cur_a, cur_b = headA, headB # 用两个指针代替a和b
while cur_a != cur_b:
cur_a = cur_a.next if cur_a else headB
# 如果a走完了,那么就切换到b走
# if cur_a:
# cur_a = cur_a.next
# else:
# cur_a = headA
cur_b = cur_b.next if cur_b else headA # 同理,b走完了就切换到a
return cur_a
# 8
# headA:[4,1,8,4,5]
# headB:[5,0,1,8,4,5]
# skipA = 2
# skipB = 3
# 走法:
# 4 1 8 4 5 4 1 8 4 5 4 1 8 4 5 4 1 8 4 5 4 1 8 4 5 4 1 8 4 5 headA走了6遍
# 5 0 1 8 4 5 5 0 1 8 4 5 5 0 1 8 4 5 5 0 1 8 4 5 5 0 1 8 4 5 headB走了5遍
# 5 * 6 = 6 * 5
142.环形链表II
力扣题目链接: https://leetcode.cn/problems/linked-list-cycle-ii/
题意: 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
为了表示给定链表中的环,使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -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, head
while fast and fast.next: # 快慢指针相遇问题
fast = fast.next.next
slow = slow.next
if fast == slow: # 相遇后,从起点设置一个指针,以及相遇点也设置一个,同时移动,在环的入口处相遇
index1 = fast
index2 = head
while index1 is not index2:
index1 = index1.next
index2 = index2.next
return index2
return None
快指针先入环,可能跑了好几圈,也可能一圈还没跑完,慢指针入环时,快指针可能在环的任意位置,然后快指针开始追击慢指针