![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
链表
一杯小熊
这个作者很懒,什么都没留下…
展开
-
Leetcode 61 旋转链表
思路:先连成环再截断代码:# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def rotateRight(self, head: ListNode, k: int) -> ListNode: ..原创 2021-06-19 17:23:38 · 66 阅读 · 0 评论 -
二叉树展开为链表
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def flatten(self, root: TreeNode) ->...原创 2021-04-07 10:39:15 · 62 阅读 · 0 评论 -
LeetCode. 141. 环形链表
方法1:哈希表,空间复杂度O(n)方法2:快慢指针,龟兔赛跑# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def hasCycle(self, head: ListNode) -> bool: #时间复杂度O..原创 2021-03-13 22:54:35 · 51 阅读 · 0 评论 -
剑指Offer 52: 两个链表的第一个公共节点
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: node1, n...原创 2021-03-09 10:28:07 · 73 阅读 · 0 评论 -
剑指Offer 35. 复杂链表的复制
"""# Definition for a Node.class Node: def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None): self.val = int(x) self.next = next self.random = random"""class Solution: def copyRandomList(self, head: '...原创 2021-03-05 10:58:33 · 78 阅读 · 1 评论 -
剑指Offer 24 反转链表
迭代的思想就是利用pre和cur两个指针,然后逐步向后遍历# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def reverseList(self, head: ListNode) -> ListNode: #递归 ..原创 2021-02-27 10:56:46 · 156 阅读 · 0 评论 -
删除链表的倒数第n个节点/LeetCode.No.19
用快慢指针,时间复杂度O(n), 空间复杂度O(1)# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def removeNthFromEnd(self, head: ListNode, n: int) -&g..原创 2021-01-20 21:24:16 · 65 阅读 · 0 评论 -
LeetCode.No.237:删除链表中的节点
代码:# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def deleteNode(self, node): """ :type node: ListNode :rtype: void D...原创 2021-01-10 22:30:46 · 56 阅读 · 0 评论 -
Python实现一个二叉树
class BinaryTree(object): def __init__(self, node): self.val = node self.left = None self.right = None def insertleft(self, node): self.left = BinaryTree(node) return self.left def ins.原创 2021-01-03 16:26:05 · 106 阅读 · 0 评论 -
Python实现一个单链表
class Node(object): '''定义一个节点类''' def __init__(self, elem): self.val = elem self.next = None class SingleLinkList(object): '''定义一个单链表''' def __init__(self, node = None): self.head = node def is...原创 2021-01-03 16:22:47 · 200 阅读 · 0 评论