代码随想录day4|24. 两两交换链表中的节点19. 删除链表的倒数第 N 个结点面试题 02.07. 链表相交142. 环形链表 II

# 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:
            temp3=cur.next.next.next
            temp1=cur.next
            cur.next = cur.next.next
            cur.next.next = temp1
            temp1.next = temp3
            cur = temp1
        return dummy_head.next

两两交换链表中的节点,要注意while中的条件,并不是or(分别判断奇偶两种情况)而是and只判断偶数节点的情况,因为奇数节点不需要进行交换 

# 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_node = ListNode(next=head)
        slow = fast= dummy_node
        for _ in range(n):
            fast = fast.next
        fast =   fast.next
        while fast:
            slow = slow.next
            fast = fast.next 
        slow.next = slow.next.next 
        return dummy_node.next

 删除链表的倒数第 N 个结点,双指针法两个类型,第一个逐渐缩小区域,两指针从两边往中间缩,第二个快慢指针,一个做遍历一个做游标

# 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:
        size_a =size_b= 0
        cur = headA
        while cur:
            cur = cur.next 
            size_a+=1
        cur = headB
        while cur:
            cur = cur.next 
            size_b+=1
        if size_a >=size_b:
            _len = size_a - size_b
            for _ in range(_len):
                headA = headA.next
            while headA:
                if headA == headB:
                    return headA
                headA = headA.next
                headB = headB.next
            return 
        else:
            _len = size_b - size_a
            for _ in range(_len):
                headB = headB.next
            while headA:
                if headA == headB:
                    return headA
                headA = headA.next
                headB = headB.next
            return 

链表相交,要注意先判断是否相交,再headA =headA.next

# 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]:
        dummy_node = ListNode(next = head)
        fast = dummy_node
        slow = dummy_node
        index = dummy_node
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
            if fast == slow:
                while index != slow:
                    slow = slow.next
                    index = index.next 
                return index
        return

环形链表,直接找个例子带一下就行,不需要数学推导,依旧是快慢指针

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值