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

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

对于题目想法:希望通过指针,直接更换两个节点,但看了卡哥视频后加入虚拟头节点。代码如下:

# Definition for singly-linked list.
# 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
        while cur.next and cur.next.next:
            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
        

使用递归代码如下:

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

class Solution:
    def swapPairs(self, head: ListNode) -> ListNode:
        if head == None or head.next == None:
            return head
        pre = head
        cur = head.next
        next = cur.next
        cur.next = pre
        pre.next = self.swapPairs(next)
        return cur

运行结果:

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

对于题目想法:先判断节点是否在链表内,再得到链表长度,得到正序位置,再通过遍历得到需要删除的节点。最后对节点直接跳过进行删除。但 由于没有size,而自己思路并不清洗,希望通过设置多个计数值转换为找第几个节点。故看卡哥视频,了解需要用间隔为n+1的快慢指针。代码如下:

# Definition for singly-linked list.
# 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]:
        dummy_head = ListNode(next =head)
        fast = slow = dummy_head
        for _ in range(n+1):
            fast = fast.next
        while fast:
            fast = fast.next
            slow = slow.next
        slow.next = slow.next.next
        return dummy_head.next

 结果:

 三、面试题 02.07. 链表相交

对于题目想法:首先通过判断headA和headB的最后一个节点是否相等来确定两个列表是否相交,然后通过反转列表后双指针来判断列表相交节点。代码如下:

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

class Solution:
    def reverseList(self, head: ListNode):
        cur = head
        pre = None
        while cur:
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp
        return pre
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        List1, List2 = headA, headB
        while List1.next:
            List1 = List1.next
        while List2.next:
            List2 = List2.next
        if List1 != List2:
            return None
        headA1 = self.reverseList(headA)
        headB1 = self.reverseList(headB)
        prev = None
        while headA1 and headB1 and headA1 != headB1:
            prev = headA1
            headA1 = headA1.next
            headB1 = headB1.next
        return prev
       

 

但反转列表后已经改变节点,故我如何调整都不能解决问题。

故看卡哥,使用两列表长度相减解决此题。解答如下:

# 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:
        LenA,LenB = 0,0
        cur = headA
        while cur:
            LenA +=1
            cur = cur.next
        cur = headB
        while cur:
            LenB +=1
            cur = cur.next
        curA ,curB = headA,headB
        if LenB >LenA:
            curA ,curB = headB,headA
            LenA,LenB = LenB,LenA
        for _ in range(LenA-LenB):
            curA = curA.next

        while curA:
            if curA ==curB:
                return curA
            curA = curA.next
            curB = curB.next
        
        return None    
            

结果: 

 142.环形链表II :

对于题目想法:题目拿到并无太大思路,看了卡哥视频后用快慢指针进行处理,代码如下:

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

class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        fast = head
        slow = head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
            if fast == slow:
                indext2 = head
                while fast !=indext2:
                    fast = fast.next
                    indext2 = indext2.next
                return fast
        
        return None
       

结果:

总结:链表逻辑暂且清晰,但对于双指针运用依旧需要加强

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值