首页 > 其他分享 >代码随想录Day4 | LeetCode 24. 两两交换链表中的节点、LeetCode 19. 删除链表的倒数第 N 个结点、LeetCode 160. 相交链表

代码随想录Day4 | LeetCode 24. 两两交换链表中的节点、LeetCode 19. 删除链表的倒数第 N 个结点、LeetCode 160. 相交链表

时间:2024-09-17 23:01:37浏览次数:9  
标签:p2 head p1 ListNode self 随想录 next 链表 LeetCode

LeetCode 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]:
        if not head or not head.next:
            return head
        pre = self.swapPairs(head.next.next)
        next = head.next
        head.next = pre
        next.next = head
        return next

LeetCode 25. K 个一组翻转链表

一道拓展

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
        if not head:
            return head
        p1 = p2 = head
        for _ in range(k):
            if not p2:
                return head
            p2 = p2.next
        pre = self.reverseKGroup(p2, k)
        while p1 != p2:
            next = p1.next
            p1.next = pre
            pre = p1
            p1 = next
        return pre

LeetCode 19. 删除链表的倒数第 N 个结点

双指针

# 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]:
        if not head:
            return head
        dummy = ListNode()
        dummy.next = head
        p1 = p2 = dummy
        for _ in range(n):
            p2 = p2.next
        while p2.next:
            p1 = p1.next
            p2 = p2.next
        p1.next = p1.next.next
        return dummy.next

LeetCode 160. 相交链表

# 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) -> Optional[ListNode]:
        p1, p2 = headA, headB
        while p1 != p2:
            p1 = p1.next if p1 else headB
            p2 = p2.next if p2 else headA
        return p1

LeetCode 142. 环形链表 II

巧用双指针,建议先看下面的 LeetCode 141. 环形链表

# 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]:
        if not head or not head.next:
            return None
        p1 = p2 = head
        while p2 and p2.next:
            p1 = p1.next
            p2 = p2.next.next
            if p1 == p2:
                p2 = head
                break
        if not p2 or not p2.next:
            return None
        while p1 != p2:
            p1 = p1.next
            p2 = p2.next
        return p2

LeetCode 141. 环形链表

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

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        if not head:
            return False
        p1 = p2 = head
        while p2 and p2.next:
            p1 = p1.next
            p2 = p2.next.next
            if p1 == p2:
                return True

        return False

标签:p2,head,p1,ListNode,self,随想录,next,链表,LeetCode
From: https://www.cnblogs.com/li508q/p/18417702

相关文章