
算法-链表
sparksnail
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LintCode 450. Reverse Nodes in k-Group
题目 思路 1.使用栈,把K个node入栈,然后按照出栈的顺序重新连接。 2.原地翻转,加一个头结点dummyNode便于操作。 代码1 """ Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next...原创 2018-02-19 00:29:18 · 190 阅读 · 0 评论 -
LeetCode 141. Linked List Cycle
题目 思路 快慢指针。 代码 # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def ...原创 2018-03-31 02:04:34 · 155 阅读 · 0 评论 -
LeetCode 143. Reorder List
题目 思路 1.拆分链表 2.后边链表就地逆置 3.merge 代码 # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Soluti...原创 2018-03-31 01:48:53 · 155 阅读 · 0 评论 -
LeetCode 147. Insertion Sort List
题目 Sort a linked list using insertion sort. 思路 实现了插入排序和选择排序。 不过,两段代码在一个case上都TLE了。 代码1(插入排序) # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # ...原创 2018-03-30 23:33:48 · 131 阅读 · 0 评论 -
LeetCode 148. Sort List
题目 Sort a linked list in O(n log n) time using constant space complexity. 思路 链表的归并排序 代码 # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # sel...原创 2018-03-30 22:30:01 · 135 阅读 · 0 评论 -
LintCode 372. Delete Node in the Middle of Singly Linked List
题目 思路 复制数字即可。 代码 """ Definition of ListNode class ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next """ class Solution: """ ...原创 2018-02-22 14:49:35 · 209 阅读 · 0 评论 -
LintCode 106. Convert Sorted List to Balanced BST
题目 思路 递归构建左子树和右子树 代码 """ Definition of ListNode class ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next Definition of TreeNode: clas...原创 2018-02-22 14:41:19 · 187 阅读 · 0 评论 -
LintCode 98. Sort List
题目 思路 归并排序,链表。 代码 """ Definition of ListNode class ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next """ class Solution: """ ...原创 2018-02-22 14:27:23 · 270 阅读 · 0 评论 -
LintCode 102. Linked List Cycle
题目 思路 两个指针同时遍历即可。 代码 """ Definition of ListNode class ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next """ class Solution: """ ...原创 2018-02-21 21:21:49 · 196 阅读 · 0 评论 -
LintCode 170. Rotate List
题目 思路 K有大于链表长度的情况,需要取余处理。 代码 """ Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None """ class Solution: """ @...原创 2018-02-21 17:44:35 · 214 阅读 · 0 评论 -
LintCode 99. Reorder List
题目 思路 用堆栈,反向得到链表的结点。 代码 """ Definition of ListNode class ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next """ class Solution: ""...原创 2018-02-21 17:21:41 · 215 阅读 · 0 评论 -
LintCode 511. Swap Two Nodes in Linked List
题目 思路 遍历,找到nodeV1和nodeV2,交换即可。 注意noveV1和nodeV2相邻的情况。 代码 """ Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None """ c...原创 2018-02-21 17:04:37 · 254 阅读 · 0 评论 -
LintCode 36. Reverse Linked List II
题目 思路 水题,遍历翻转即可。 代码 """ Definition of ListNode class ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next """ class Solution: """ ...原创 2018-02-21 16:49:46 · 199 阅读 · 0 评论 -
LintCode 165. Merge Two Sorted Lists
题目 思路 水题 代码 """ Definition of ListNode class ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next """ class Solution: """ @para...原创 2018-02-19 01:22:15 · 350 阅读 · 0 评论 -
LintCode 96. Partition List
题目 思路 用两个指针,记录下来nmax和pre_nmax结点,之后不断插入即可。 代码 """ Definition of ListNode class ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next """ ...原创 2018-02-19 01:14:10 · 245 阅读 · 0 评论 -
Leet 138. Copy List with Random Pointer
题目 A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路 先复制next结点,然后再复制random结点。 ...原创 2018-04-01 01:39:13 · 168 阅读 · 0 评论