24. 两两交换链表中的节点
#伪代码
cur要指向 需要反转的两个节点的前一个节点
dummyhead.next=head
cur=dummyhead
#一定是cur.next!=NULL&&cur.next.next!=NULL的顺序,否则,cur.next.next!=NULL&&cur.next!=NULL的话,如果cur.next=NULL,则cur.next.next!=NULL
while(cur.next!=NULL&&cur.next.next!=NULL){
#两两交换的逻辑
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 dummyhead.next
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy_head=ListNode(next=head)
cur=dummy_head
while(cur.next!=None and cur.next.next!=None):
temp=cur.next
temp1=cur.next.next.next
cur.next=cur.next.next
cur.next.next=temp
temp.next=temp1
#这里是cur=cur.next.next 而不是cur.next=cur.next.next cur不断往后移动更新
cur=cur.next.next
return dummy_head.next
19.删除链表的倒数第N个节点
#伪代码
fast=dummyhead
slow=dummyhead
n++
while(n--&&fast!=NULL){
fast=fast.next
}
while(fast!=NULL){
fast=fast.next
slow=slow.next
}
slow.next=slow.next.next
return dummyhead.next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dummy_head=ListNode(next=head)
fast=dummy_head
slow=dummy_head
n+=1
while(n>0 and fast!=None):
fast=fast.next
n-=1
while(fast!=None):
fast=fast.next
slow=slow.next
slow.next=slow.next.next
return dummy_head.next
面试题 02.07. 链表相交
# 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:
cur=headA
lenA=0
while(cur!=None):
cur=cur.next
lenA+=1
cur=headB
lenB=0
while(cur!=None):
cur=cur.next
lenB+=1
curA=headA
curB=headB
if(lenA>lenB):
temp1=curB
curB=curA
curA=temp1
temp2=lenB
lenB=lenA
lenA=temp2
for _ in range(lenB-lenA):
curB=curB.next
while(curA!=None):
if(curA==curB):
return curA
else:
curA = curA.next
curB = curB.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:
cur=headA
lenA=0
while(cur!=None):
cur=cur.next
lenA+=1
cur=headB
lenB=0
while(cur!=None):
cur=cur.next
lenB+=1
curA=headA
curB=headB
if(lenA>lenB):
temp1=curB
curB=curA
curA=temp1
temp2=lenB
lenB=lenA
lenA=temp2
for _ in range(lenB-lenA):
curB=curB.next
while(curA!=None):
if(curA==curB):
return curA
else:
curA = curA.next
curB = curB.next
return None
142.环形链表II
#伪代码
一个快指针,一个满指针,如果相遇的话就会有环
双指针法
fast:每次走两个节点
slow:每次走一个节点
fast一定可以遇到slow
(如果fast每次走三个节点是不行的,可能会跳过)
#找到环的入口
slow=x+y
fast=x+y+n(y+z)
2*slow=fast
2*(x+y)=x+y+n(y+z)
x=n(y+z)-y n>=1
x=(n-1)(y+z)+z n>=1
当n=1时,x=z
#code
fast=head
slow=head
while(fast!=NULL&&fast.next!=NULL){
fast=fast.next.next
slow=slow.next
if(fast=slow){
index1=fast
index2=head
while(index1!=index2){
index1=index1.next
index2=index2.next
}
return index1;
}
}
return NULL;
class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
#双指针
fast=head
slow=head
while(fast!=None and fast.next!=None):
fast=fast.next.next
slow=slow.next
if(fast==slow):
index1=fast
index2=head
while(index1==index2):
index1=index1.next
index2=index2.next
return index1
return None