Leetcode 24 Swap Nodes in Pairs
- 题目链接:leetcode.com/problems/sw…
- 文章讲解:代码随想录 programmercarl.com
- 视频讲解:帮你把链表细节学清楚! | LeetCode:24. 两两交换链表中的节点
- 状态:没做出来
1. 第一想法
想不出怎么交换。
2. 看完后想法
# 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)
cur = dummy_head
while cur.next and cur.next.next:
temp = cur.next
temp1 = cur.next.next.next
cur.next = cur.next.next
cur.next.next = temp
temp.next = temp1
cur = cur.next.next
return dummy_head.next
3. 总结
画图确实会清晰很多。
Leetcode 19 Remove Nth Node From End of List
- 题目链接:leetcode.com/problems/re…
- 文章讲解:代码随想录 programmercarl.com
- 视频讲解:链表遍历学清楚! | LeetCode:19.删除链表倒数第N个节点
- 状态:Wrong Answer
1. 第一想法
我看了 hint, 知道要用两个差 n 的指针。一开始没有这个代码的第11行,是Runtime Error。尝试在 while 里加了一些判断空不空的,没啥用。后来加了这行是Wrong Answer。输入都是[1] 1,后来我的输出是[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]:
dummy_head = ListNode(next = head)
fast = head
for i in range(n):
if fast.next:
fast = fast.next
slow = head
while fast and fast.next:
fast = fast.next
slow = slow.next
if slow and slow.next:
slow.next = slow.next.next
while slow and slow.next:
slow = slow.next
return dummy_head.next
2. 看完后想法
之前那个情况,slow 和 fast 都会停在要删的节点上,所以删不掉。他们都 dummy 起始就没问题了,这里贴修改后的代码。
# 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 = dummy_head
for i in range(n):
if fast.next:
fast = fast.next
slow = dummy_head
while fast and fast.next:
fast = fast.next
slow = slow.next
if slow.next:
slow.next = slow.next.next
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 removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dummy_head = ListNode(0, head)
slow = fast = dummy_head
for i in range(n+1):
fast = fast.next
while fast:
slow = slow.next
fast = fast.next
slow.next = slow.next.next
return dummy_head.next
3. 总结
之前的版本默认被删除的节点前有非dummy的节点,会导致没法删除第一个节点的情况。还是要记住用虚拟头节点可以统一解决很多问题。
Leetcode 160 Intersection of Two Linked Lists
- 题目链接:leetcode.com/problems/in…
- 文章讲解:代码随想录 programmercarl.com
- 状态:有做出来
1. 第一想法
放到 set 里,看第二个 linked list 会不会有重复的节点。
# 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) -> Optional[ListNode]:
s = set()
while headA:
s.add(headA)
headA = headA.next
while headB:
if headB in s:
return headB
else:
headB = headB.next
return None
2. 看完后想法
这是一个很人类的解法啊。
# 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) -> Optional[ListNode]:
lenA, lenB = 0, 0
cur = headA
while cur:
cur = cur.next
lenA += 1
cur = headB
while cur:
cur = cur.next
lenB += 1
curA, curB = headA, headB
if lenA > lenB:
curA, curB = curB, curA
lenA, lenB = lenB, lenA
for _ in range(lenB - lenA):
curB = curB.next
while curA:
if curA == curB:
return curA
else:
curA = curA.next
curB = curB.next
return None
3. 总结
这题简单一点。
Leetcode 142 Linked List Cycle II
- 题目链接:leetcode.com/problems/li…
- 文章讲解:代码随想录 programmercarl.com
- 视频讲解:把环形链表讲清楚! 如何判断环形链表?如何找到环形链表的入口? LeetCode:142.环形链表II
- 状态:没做出来
1. 第一想法
我知道怎么判断有没有圈,就是一个普通指针一个二倍速指针,撞一起就有圈了。但我不知道怎么找那个交点。感觉是个数学问题,以前做过,我又忘了。
2. 看完后想法
哦对我怎么没想到这个也可以用 set。单纯想着“我想不起那个恨数学的做法”。
# 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 = head
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 slow
return None
3. 总结
常看常新。