代码随想录算法训练营第四天-链表part2

day2和day3回家太晚,刷完题忘记写笔记了 - -!

24.两两交换链表中的节点

给自己的笔记:
虚拟节点法是创建一个节点,它的next指针指向链表的头节点,这样便于:

  1. current指向虚拟节点,然后对链表进行操作交换
  2. 最后返回头节点:return dummyNode.next
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        #创建虚拟节点
        dummyNode=ListNode(next=head)
        #curr 指向dummyNode
        curr=dummyNode
        while(curr.next and curr.next.next):#cur的下一位和下下一位都在 才会进入循环
            temp=curr.next
            temp1=curr.next.next.next
            curr.next=curr.next.next
            curr.next.next=temp
            temp.next=temp1
            curr=curr.next.next#curr往前2位
        return dummyNode.next #返回头节点
       

19 删除链表的倒数第N个节点

  1. 笨办法,先遍历所有节点获得链表长度。
class Solution(object):
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        #虚拟头节点
        dummyNode=ListNode(next=head)
        curr=dummyNode
        sizeLink=0
        count=0
        while(curr.next):
            sizeLink+=1
            curr=curr.next
        x=sizeLink-n
        curr=dummyNode
        if(sizeLink==1):
            return None
        if(sizeLink==n):
            return head.next
        while(curr.next):
            curr=curr.next
            count+=1
            if(count==x):
                curr.next=curr.next.next   
        return dummyNode.next
  1. 双指针法
    tips:数组也是可以这样的思路
class Solution(object):
    def removeNthFromEnd(self, head, n):
        dummyNode=ListNode(next=head)
        slow=dummyNode
        fast=slow
        curr=dummyNode
        for i in range(n+1):#注意是n+1,这样快指针走到尾,慢指针停在要删的节点前一个节点
            fast=fast.next
        
        while(fast!=None):
            fast=fast.next
            slow=slow.next
        slow.next=slow.next.next
        return dummyNode.next

面试题 02.07. 链表相交

核心思想是求两个链表长度差,让指向长链表头指针移动到,和短链表头指针对齐的位置
比如:
A: [4,1,8,4,5]
currA->4
B:[5,0,1,8,4,5]
currB->0

class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        dummyNodeA=ListNode(next=headA)
        currA=dummyNodeA
        sizeA,sizeB=0,0
        dummyNodeB=ListNode(next=headB)
        currB=dummyNodeB
        curr,curr2=headA,headB
        while curr:
            curr=curr.next
            sizeA+=1
        while curr2:
            curr2=curr2.next
            sizeB+=1
        diff=abs(sizeA-sizeB)#长度差
        
        for i in range(diff):#让长链表指针移动到,和短链表头指针对齐的位置
            if sizeA>sizeB:
                currA=currA.next
            else:
                currB=currB.next
        
        while currA=currB #当指针相等
            currA=currA.next
            currB=currB.next
        return currA
        
        

142.环形链表II

…待补充

你可能感兴趣的:(算法,链表,数据结构)