LeetCode: 高频链表题目总结 - Python

LeetCode:高频链表题目总结

问题描述:

  1. LeetCode: 2. 两数相加 , 注意是逆序存储,相加求和
  2. LeetCode: 19. 删除链表的倒数第 N 个结点
  3. LeetCode: 21. 合并两个有序链表
  4. LeetCode: 23. 合并 K 个升序链表
  5. LeetCode: 24. 两两交换链表中的节点 ,两两相邻的节点交换
  6. LeetCode: 25. K 个一组翻转链表
  7. LeetCode: 61. 旋转链表
  8. LeetCode: 83. 删除排序链表中的重复元素 ,已经排序,只保留一个
  9. LeetCode: 82. 删除排序链表中的重复元素 II ,已经排序,一个不保留
  10. LeetCode: 86. 分隔链表 ,根据一个元素判断,小的在前面大的在后面
  11. LeetCode: 92. 反转链表 II ,给定一个区间[left <= right]进行反转
  12. LeetCode: 114. 二叉树展开为链表 ,先序遍历类型
  13. LeetCode: 138. 复制带随机指针的链表
  14. LeetCode: 141. 环形链表 ,判断是否存在环
  15. LeetCode: 142. 环形链表 II ,找环的入口
  16. LeetCode: 143. 重排链表 ,首尾交替链接类型
  17. LeetCode: 148. 排序链表 ,由小到大排序,归并排序即可
  18. LeetCode: 160. 相交链表 ,求两个链表的交点
  19. LeetCode: 206. 反转链表 ,头插发
  20. LeetCode: 234. 回文链表 ,输出反转
  21. 剑指 Offer 18. 删除链表的节点 ,正常删除类型
  22. 剑指 Offer 22. 链表中倒数第k个节点 ,查找类型

(1)LeetCode: 2. 两数相加 Python3实现:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
        carry = 0
        notehead = ListNode(0)
        curr = notehead
        while l1 != None or l2 != None:
            n = l1.val if l1 != None else 0
            m = l2.val if l2 != None else 0

            sum = carry + n + m

            carry = int(sum/10)
            curr.next = ListNode(sum%10)

            curr = curr.next

            if l1 != None: l1 = l1.next   # 进入下一个
            if l2 != None: l2 = l2.next 
        
        if carry > 0:
            curr.next = ListNode(carry)
        
        return notehead.next

(2)LeetCode: 19. 删除链表的倒数第 N 个结点 Python3实现:

class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        res = ListNode(0, head)
        fist = head
        second = res
        for _ in range(n):
            fist = fist.next
        while fist:
            fist = fist.next
            second = second.next
        second.next = second.next.next
        return res.next

(3)LeetCode: 21. 合并两个有序链表 Python3实现:

class Solution:
    def mergeTwoLists(self, list1, list2):
        if not list1: return list2
        if not list2: return list1
        if list1.val < list2.val:
            l3, list1 = list1, list1.next
        else:
            l3, list2 = list2, list2.next
        cur = l3
        while list1 and list2:
            if list1.val < list2.val:
                cur.next, list1 = list1, list1.next
            else:
                cur.next, list2 = list2, list2.next
            cur = cur.next
        cur.next = list1 if list1 else list2
        return l3

(4)LeetCode: 23. 合并 K 个升序链表 Python3实现:

class Solution:

    def mergeTwoLists(self, list1, list2):

        if not list1: return list2
        if not list2: return list1
        if list1.val < list2.val: 
            l3, list1 = list1, list1.next
        else:
            l3, list2 = list2, list2.next
        cur = l3
        while list1 and list2:
            if list1.val < list2.val:  
                cur.next, list1 = list1, list1.next
            else:  
                cur.next, list2 = list2, list2.next
            cur = cur.next
        cur.next = list1 if list1 else list2
        return l3

    def mergeKLists(self, lists):

        if len(lists) == 0:
            return None
        if len(lists) == 1:
            return lists[0]
        k = len(lists)
        res = lists[0]
        for i in range(1, k):

            res = self.mergeTwoLists(res, lists[i])

        return res

(5)LeetCode: 24. 两两交换链表中的节点 Python3实现:

class Solution:
    def swapPairs(self, head):
        if not head: return head
        res = ListNode(0, head)
        c = res
        while c.next and c.next.next:
            a, b = c.next, c.next.next
            c.next, a.next = b, b.next
            b.next = a
            c = c.next.next
        return res.next

(6)LeetCode: 25. K 个一组翻转链表 Python3实现:

class Solution:
    # 翻转一个子链表,并且返回新的头与尾
    def reverse(self, head: ListNode, tail: ListNode):
        prev = tail.next
        p = head
        while prev != tail:
            nex = p.next
            p.next = prev
            prev = p
            p = nex
        return tail, head

    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        hair = ListNode(0)
        hair.next = head
        pre = hair

        while head:
            tail = pre
            # 查看剩余部分长度是否大于等于 k
            for i in range(k):
                tail = tail.next
                if not tail:
                    return hair.next
            nex = tail.next
            head, tail = self.reverse(head, tail)
            # 把子链表重新接回原链表
            pre.next = head
            tail.next = nex
            pre = tail
            head = tail.next
        
        return hair.next

(7)LeetCode: 61. 旋转链表 Python3实现:

class Solution:
    def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:

        if k == 0 or not head or not head.next:
            return head
        cur = head
        n = 1
        while cur.next:
            cur = cur.next
            n += 1
        add = n - k % n 
        if add == n:  # 倍数的时候
            return head
        cur.next = head
        while add:
            cur = cur.next
            add -= 1
        res = cur.next
        cur.next = None
        return res

(8)LeetCode: 83. 删除排序链表中的重复元素 Python3实现:

class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head:
            return head
        cur = head
        while cur.next:
            if cur.next.val == cur.val:
                cur.next = cur.next.next
            else:
                cur = cur.next
        return head

(9)LeetCode: 82. 删除排序链表中的重复元素 II Python3实现:

class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:

        if not head:
            return head
        pre = ListNode(0, head)
        cur = pre
        while cur.next and cur.next.next:
            if cur.next.val == cur.next.next.val:
                x = cur.next.val
                while cur.next and cur.next.val == x:
                    cur.next = cur.next.next
            else:
                cur = cur.next
        return pre.next

(10)LeetCode: 86. 分隔链表 Python3实现:

class Solution:
    def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
        small = ListNode()
        large = ListNode()
        
        pre_small = small
        pre_large = large
        
        while head:
            if head.val < x:
                small.next = head
                small = small.next
            else:
                large.next = head
                large = large.next
            
            head = head.next
        
        large.next = None
        small.next = pre_large.next
        return pre_small.next

(11)LeetCode: 92. 反转链表 II Python3实现:

class Solution:
    def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:

        pre_head = ListNode(0, head)
        pre = pre_head

        for _ in range(left -1):
            pre = pre.next

        cur = pre.next

        for _ in range(right - left):
            tmp = cur.next
            cur.next = cur.next.next
            
            tmp.next = pre.next
            pre.next = tmp
        
        return pre_head.next

(12)LeetCode: 114. 二叉树展开为链表 Python3实现:

class Solution:
    def flatten(self, root: Optional[TreeNode]) -> None:
        """
        Do not return anything, modify root in-place instead.
        """

        res = []

        def dfs(root):
            if  root:
                res.append(root)
                if root.left: dfs(root.left)
                if root.right: dfs(root.right)
        dfs(root)

        n = len(res)
        for i in range(1, n):
            pre, cur = res[i-1], res[i]
            pre.left = None
            pre.right = cur

(13)LeetCode: 138. 复制带随机指针的链表 Python3实现:

class Solution:
    def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
        my_dict = {}
        def copynode(node):
            if node is None: return None
            if my_dict.get(node): 
                return my_dict.get(node)
            root = Node(node.val)  # 存入字典
            my_dict[node] = root
            root.next = copynode(node.next)  # 继续获取下一个节点
            root.random = copynode(node.random)  # 继续获取下一个随机节点
            return root

        return copynode(head)

(14)LeetCode: 141. 环形链表 Python3实现:

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        if not head or not head.next:
            return False
        slow = head
        fast = head.next
        while slow != fast:
            if not fast or not fast.next:
                return False
            slow = slow.next
            fast = fast.next.next
        return True

(15)LeetCode: 142. 环形链表 II Python3实现:

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

(16)LeetCode: 143. 重排链表 Python3实现:

class Solution:
    def reorderList(self, head):
        if not head: return
        ltmp = []
        cur = head
        while cur:  # 转换成列表
            ltmp.append(cur)
            cur = cur.next
        n = len(ltmp)  # 链表长度
        for i in range(n // 2):  # 处理
            ltmp[i].next = ltmp[n - 1 - i]
            ltmp[n - 1 - i].next = ltmp[i + 1]
        ltmp[n // 2].next = None  # 最后一个指向空

(17)LeetCode: 148. 排序链表 Python3实现:

class Solution:
    def sortList(self, head: ListNode) -> ListNode:
        if not head or not head.next:
            return head
        slow = head
        fast = head

        while fast.next and fast.next.next:  # 用快慢指针分成两部分
            slow = slow.next
            fast = fast.next.next

        mid = slow.next  # 找到左右部分, 把左部分最后置空
        slow.next = None

        left = self.sortList(head)   # 递归下去
        right = self.sortList(mid)

        return self.merge(left, right)  # 合并

    def merge(self, left, right):
        dummy = ListNode(0)
        p = dummy
        l = left
        r = right

        while l and r:
            if l.val < r.val:
                p.next = l
                l = l.next
                p = p.next
            else:
                p.next = r
                r = r.next
                p = p.next
        if l:
            p.next = l
        if r:
            p.next = r
        return dummy.next

(18)LeetCode: 160. 相交链表 Python3实现:

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
        
        if headA is None or headB is None:
            return None
        pa = headA
        pb = headB
        while pa != pb:
            pa = pa.next if pa else headB
            pb = pb.next if pb else headA
        return pa

(19)LeetCode: 206. 反转链表 Python3实现:

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:

        if not head: return head
        
        pre = None
        cur = head
        next = head.next
        
        while next:
            cur.next = pre
            pre = cur
            cur = next
            next = next.next
        cur.next = pre
        
        return cur

(20)LeetCode: 234. 回文链表 Python3实现:

class Solution:
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        
        vals = []
        cur = head
        while cur:
            vals.append(cur.val)
            cur = cur.next
        return vals == vals[::-1]

(21)剑指 Offer 18. 删除链表的节点 Python3实现:

class Solution:
    def deleteNode(self, head: ListNode, val: int) -> ListNode:

        if head is None:
            return head
        
        pre = ListNode(0)
        pre.next = head
        cur = pre
        while cur and cur.next:
            if cur.next.val == val:
                cur.next = cur.next.next
            cur = cur.next
        return pre.next

(22)剑指 Offer 22. 链表中倒数第k个节点 Python3实现:

class Solution:
    def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:

        res = ListNode(0)
        res.next = head
        fist = head
        second = res
        for _ in range(k):
            fist = fist.next
        while fist:
            fist = fist.next
            second = second.next
        return second.next

相关参考:
声明: 总结学习,有问题或不当之处,可以批评指正哦,谢谢。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值