title: LEETCODE DAY4
date: 2024-02-25 11:00:40
tags:
DAY2
今日题目:LEETCODE 24(两两交换链表中的节点)、19(删除链表的倒数第N个节点)、160 链表相交 (面试题 02.07.) 、142(环形链表II)
附加题:
T1 两两交换链表中的节点 mark
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.next
pre=dummy_head.next
while cur.next and cur.next.next:
cur=cur.next
temp=cur.next
pre.next=temp
cur.next=pre
pre=temp
cur=temp
return head
AttributeError: ‘NoneType’ object has no attribute ‘next’
^^^^^^^^
while cur.next and cur.next.next:
Line 16 in swapPairs (Solution.py)
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy_head=ListNode(next=head)
cur=dummy_head.next
pre=dummy_head.next
while cur and cur.next:
cur=cur.next
temp=cur.next
pre.next=temp
cur.next=pre
pre=temp
cur=temp
return head
输入
head =
[1,2,3,4]
输出
[1,3]
预期结果
[2,1,4,3]
这里逻辑有点不对,相当于是在看1 2 3 的交换(0=dummy_head)所以结果上显示1和2没有交换成功
往后看不行改成往前看(即考虑0 1 2)
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.next
pre=dummy_head
while cur and cur.next:
temp=cur.next
pre.next=temp
cur.next=temp.next
temp.next=cur
pre=cur
cur=cur.next
return head
输入
head =
[1,2,3,4]
输出
[1,4,3]
预期结果
[2,1,4,3]
修改一下输出
改为return dummy_head.next
成功AC
T2
倒数第n个节点与最后一个节点差(n-1)个脚标
这里fast比slow快n步
fast到最后一个节点时,slow在倒数第n个节点前一节点
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]:
fast=ListNode(next=head)
slow=ListNode(next=head)
for i in range(n):
fast=fast.next
while fast.next:
slow=slow.next
fast=fast.next
slow.next=slow.next.next
return head
输入
head =
[1]
n =
1
输出
[1]
预期结果
[]
用例[1,2,3,4,5] n=2,通过
修改
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dummy_head=ListNode(next=head)
fast=slow=dummy_head
for i in range(n):
fast=fast.next
while fast.next:
slow=slow.next
fast=fast.next
slow.next=slow.next.next
return dummy_head.next
AC
T3
没太搞懂为什么数值相等与指针相等不同(似乎是要看地址)
即:[4,1,8,4,5]和[5,0,1,8,4,5]的相交节点为什么不是1啊?
答:该题交点不是数值相等,而是指针相等!注意是指针哦!
假设指针P1指向[4,1,8,4,5]里的1,地址是001。
假设指针P2指向[5,0,1,8,4,5]里的1,地址是002。
虽然两个指针指向的节点值相同,但是地址不同,所以这并不是同一个节点,也就不是题目的交点。
T4
class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow=head
fast=head
i=0
while slow :
fast=slow.next
while fast:
if fast !=slow:
fast=fast.next
else:
return i
break
slow=slow.next
i+=1
超出时间限制
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
visited=set()
while head:
if head in visited:
return head
else:
visited=visited.add(head)
head=head.next
return None
TypeError: argument of type ‘NoneType’ is not iterable
^^^^^^^^^^^^^^^
if head in visited:
Line 11 in detectCycle (Solution.py)
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
AC