代码随想录算法训练营第四天| 24. 两两交换链表中的节点 19.删除链表的倒数第N个节点 面试题 02.07. 链表相交 142.环形链表II

本文介绍了三种关于链表的操作:两两交换链表中的节点,删除链表的倒数第N个节点,以及找出两个链表的相交节点。这些操作均使用了双指针技术,交换节点时注意节点的逻辑顺序,删除节点需调整指针链接,寻找相交节点则通过比较两链表长度并同步移动指针来实现。
摘要由CSDN通过智能技术生成

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值