
LinkedList
文章平均质量分 71
Dylan_Java_NYC
练很重要,总结更重要,感谢优快云给了我这么好的平台交流。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode Reverse Linked List
原题链接在这里:https://leetcode.com/problems/reverse-linked-list/Method 1 和 Method 2 都是用的Iteration.Method 1 是建一个dunmy,从前往后扫原链表,遇到一个就用改点的值建一个新的Node加在dunmy和dunmy.next之间。Time O(n), Space O(n), 因为建了一个新的lis原创 2015-08-22 04:15:48 · 753 阅读 · 0 评论 -
LeetCode Reverse Linked List II
原题链接在这里:https://leetcode.com/problems/reverse-linked-list-ii/本题是Reverse Linked List的拓展。基本思路和Reverse Linked List的Method 2 相似 扩展的地方在于本题限定了reverse的范围。需先找到reverse范围的前一个点; 翻转时while loop走一次,翻转一个点,所以用一原创 2015-08-22 05:20:23 · 256 阅读 · 0 评论 -
LeetCode Convert Sorted List to Binary Search Tree
原题链接在这里:https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/找到中点当BST的root,如此递归调用,知道只有一个点时返回由当前点生成的TreeNode,以此设为终止条件。Note: 1. 此处midList找到的是中点的前一个点,然后拆成三段,中点前一段,中点单独一个,中点后一段。2.原创 2015-09-03 03:36:29 · 517 阅读 · 0 评论 -
LeetCode Sort List
原题链接在这里:https://leetcode.com/problems/sort-list/思路: 1. 递归采用merge sort2. 通过midList找到中点,从中点和中点后一点断开3. 前后段分别递归merge sort,一直拆到剩一个点,然后再mergeNote: 1. 必须记得要断开原有list,否则会栈溢出。2. 若有偶数个点,midList找的是中间一对前原创 2015-09-03 02:59:34 · 426 阅读 · 0 评论 -
LeetCode Remove Duplicates from Sorted List II
原题链接在这里:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/本题与Remove Duplicates from Sorted List相似,不同就是要完全去掉duplicate. 所以要维护一个前指针,当cur.val == cur.next.val 时,就一直移动cur直到不同,然后pre的n原创 2015-09-02 12:25:56 · 688 阅读 · 0 评论 -
LeetCode Reorder List
原题链接在这里:https://leetcode.com/problems/reorder-list/首先想到Method 1是先设置mark=head, 然后每次翻转mark后面的list, 链接mark与反转后新的head, 之后mark = mark.next.直到mark.next == null, Time O(n^2), Space O(1). 这种方法TLE了。Method原创 2015-09-03 05:49:58 · 332 阅读 · 0 评论 -
LeetCode Insertion Sort List
原题链接在这里:https://leetcode.com/problems/insertion-sort-list/思路:设置cur = head,判断cur.val 和 cur.next.val 大小关系,若是后者小, 就从头往后扫值,一直到有一点的val大于cur.next.val, 然后把cur.next这个点加在那里。Note: 1. 有可能会更改head,所以加一个dummy h原创 2015-09-03 01:16:18 · 432 阅读 · 0 评论 -
LeetCode Remove Duplicates from Sorted List
原题链接在这里:https://leetcode.com/problems/remove-duplicates-from-sorted-list/思路很简单,维护双指针,pre 和 cur的值相等就移动cur,不等就移动pre.AC Java:/** * Definition for singly-linked list. * public class ListNode { *原创 2015-09-02 12:19:11 · 447 阅读 · 0 评论 -
LeetCode Linked List Cycle II
原题链接在这里:https://leetcode.com/problems/linked-list-cycle-ii/首先找到是否有cycle,若是没有返回null。若是有cycle,可以从head,用two pointers一个一个试直到找到cycle beginner,Time O(n^2).另外一种方法参见了这篇帖子,是数学想法。AC Java:/** * Defini原创 2015-09-01 03:45:18 · 344 阅读 · 0 评论 -
LeetCode Partition List
原题链接在这里:https://leetcode.com/problems/partition-list/思路: 从头到尾每当发现大于等于x的点,就删掉加在list尾部。Note: 1. 建立一个dummy head放在链表头用来处理原有head.val大于等于x的边界情况。2.用一个mark标注原有的tail,while loop 的条件是iter.next != mark 而不是原创 2015-09-01 04:28:25 · 335 阅读 · 0 评论 -
LeetCode Linked List Cycle
原题链接在这里:https://leetcode.com/problems/linked-list-cycle/两个指针,一快runner,每次走两步;一慢walker,每次走一步,若是它俩相遇就是有cycle。Note: 这里的while loop 终止条件是runner!=null && runner.next!=null, 因为下面会用到 runner.next.next. 这类原创 2015-08-29 22:12:16 · 285 阅读 · 0 评论 -
LeetCode Swap Nodes in Pairs
原题链接在这里:https://leetcode.com/problems/swap-nodes-in-pairs/需要建一个dunmy,最后return dunmy.next. 如此可以避免边界问题的讨论,例如头两个点的转换。AC Java:/** * Definition for singly-linked list. * public class ListNode { *原创 2015-08-29 21:51:05 · 350 阅读 · 0 评论 -
LeetCode Intersection of Two Linked Lists
原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/思路: 1. 找到距离各自tail 相同距离的起始ListNode,可以通过两个pointer,长的那个先移动Math.abs(lenA-lenB).2. 两个pointer各自移动直到找到相同的ListNode.Node: 1. 若有inter原创 2015-08-29 04:07:21 · 459 阅读 · 0 评论 -
LeetCode Add Two Numbers
原题链接:https://leetcode.com/problems/add-two-numbers/思路两个val和进位carry相加,组成新点往后连,注意两个list长度不同和最后是否还有一个进位的情况。AC Java:/** * Definition for singly-linked list. * public class ListNode { * int va原创 2015-08-29 05:39:12 · 304 阅读 · 0 评论 -
LeetCode Palindrome Linked List
原题链接在这里:https://leetcode.com/problems/palindrome-linked-list/思路: 原题要求time O(n), space O(1). 所以不能用额外空间。先找到中点,reverse中点后面的list部分,再与head开始逐个比较val. 期中reverse部分可以参见Reverse Linked List中的Method 2.AC Ja原创 2015-08-29 03:19:01 · 260 阅读 · 0 评论 -
LeetCode Remove Nth Node From End of List
原题链接在这里:https://leetcode.com/problems/remove-nth-node-from-end-of-list/Method 1: 算出总长度,再减去n,即为要从头多动的点。但要新要求,only one pass。Method 2: 两个指针,一个runner,一个walker,runner先走n步,随后runner和walker一起走,直到runner指为空原创 2015-08-29 01:00:18 · 222 阅读 · 0 评论 -
LeetCode Reverse Nodes in k-Group
原题链接在这里:https://leetcode.com/problems/reverse-nodes-in-k-group/思路: 1. 检测list长度,用此长度除以k,得到的就是需要roate的次数,记为rotNum,如果rotNum等于0,则不需要rotate,直接返回head .2. 这类内部rotate都需要标记前一个点,用以连接rotate后的那段head,这个标记记为m原创 2015-08-27 02:53:15 · 308 阅读 · 0 评论 -
LeetCode Remove Linked List Elements
原题链接:https://leetcode.com/problems/remove-linked-list-elements/思路: 建一个dunmy,dunmy.next = head,为了防止head.val 等于要被踢掉的value,删掉head不方便表示。再建一个helper,这里是temp用来遍历list,每次建材temp.next 是否为null.Note: 这里犯了一个错原创 2015-08-23 00:14:11 · 284 阅读 · 0 评论