代码随想录算法训练营第四天【链表】| 24,19,160,142

文章讨论了链表操作的几种常见问题,包括在链表中交换相邻节点、删除指定位置的节点、找出两个链表的交点以及检测链表是否存在循环。使用了快慢指针法等技巧来解决这些问题并避免空指针异常。
摘要由CSDN通过智能技术生成

24. Swap Nodes in Pairs

注意:

1. cur一定要指在要操作的node的前一个node

2. while(cur.next !=None and cur.next.next !=None)顺序不能反, 不然会空指针异常

这个终止条件的意思是若是偶数个node,则cur.next==None,若是奇数个node,则cur.next.next == None, 取反的话用and

3.用temp来提前记录断掉的node

# 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
        """
        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
        return dummy_head.next

19. Remove Nth Node From End of List

注意:

1.用双指针fast和slow来找到倒数第n个节点,让fast和slow之间间隔n,这样当fast到null时slow正好到倒数第n

2.让slow指向倒数第n个节点的前一个,因此fast要走n+1步

# 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 removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        dummy = ListNode(next=head)
        slow = dummy
        fast = dummy
        n+=1
        while(n and fast!=None):
            fast = fast.next
            n-=1
        while(fast!=None):
            slow = slow.next
            fast = fast.next
        slow.next = slow.next.next
        return dummy.next

160. Intersection of Two Linked Lists

 注意这个intersection应该是指针重合而不是值相等

思路:数len,算两个len的差,将长的pointer移动此差值,然后一起向右移动直到找到intersection

【做题时先观察规律,两个linked lists的特点是末尾都相同】

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        lenA = 0 
        lenB = 0
        curA = headA
        curB = headB
        while(curA!=None):
            curA = curA.next
            lenA+=1
        while(curB!=None):
            curB = curB.next
            lenB+=1
        
        curA = headA
        curB = headB
        if lenA > lenB:
            for i in range(lenA-lenB):
                curA = curA.next
            while (curA!= None and curB != None and curA != curB):#有可能空指针异常
                curA = curA.next
                curB = curB.next
            return curA

        else:
            for i in range(lenB-lenA):
                curB = curB.next
            while (curA!= None and curB != None and curA != curB):
                curA = curA.next
                curB = curB.next
            return curB
        return None
        

142. Linked List Cycle II

快慢指针法,数学推导详见代码随想录

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值