随笔分类 -  LinkedList

摘要:Since n may be a large number compared to the length of list. So we need to know the length of linked list.After that, move the list after the (l-n%l 阅读全文
posted @ 2017-11-26 04:06 apanda009 阅读(159) 评论(0) 推荐(0)
摘要:第一想法是用HashSet<ListNode>, A list先遍历,存HashSet,然后B list遍历,发现ListNode存在就返回。但是这个方法不满足O(1)memory的要求。 再想了一会儿,略微受了点提醒,发现可以利用这个O(n) time做文章。这个条件方便我们scan list几次 阅读全文
posted @ 2017-08-19 18:20 apanda009 阅读(165) 评论(0) 推荐(0)
摘要:关于链表的题, 画图看看需要那几个节点, 一般都是接头节点(用作标记) 和遍历节点, 遍历的时候要判空, head.next != null 前一定要判head != null, 将尾节点置为空! 阅读全文
posted @ 2017-08-19 15:39 apanda009 阅读(101) 评论(0) 推荐(0)
摘要:The key of this problem is to think of using Stack, 阅读全文
posted @ 2017-08-16 22:10 apanda009 阅读(152) 评论(0) 推荐(0)
摘要:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 是在l1的基础上连接的, 所以要注意需要链接l1 l2.next = l1; 需要不断更新前一个节点pre = ... 阅读全文
posted @ 2017-08-07 17:15 apanda009 阅读(94) 评论(0) 推荐(0)
摘要:这道题是比较常见的链表反转操作,不过不是反转整个链表,而是从m到n的一部分。分为两个步骤,第一步是找到m结点所在位置,第二步就是进行反转直到n结点。反转的方法就是每读到一个结点,把它插入到m结点前面位置,然后m结点接到读到结点的下一个。总共只需要一次扫描,所以时间是O(n),只需要几个辅助指针,空间 阅读全文
posted @ 2017-08-07 16:15 apanda009 阅读(111) 评论(0) 推荐(0)
摘要:这道题跟Insertion Sort List类似,要求我们用O(nlogn)算法对链表进行排序,但是并没有要求用哪一种排序算法,我们可以使用归并排序,快速排序,堆排序等满足要求的方法来实现。对于这道题比较容易想到的是归并排序,因为我们已经做过Merge Two Sorted Lists,这是归并排 阅读全文
posted @ 2017-08-07 14:27 apanda009 阅读(134) 评论(0) 推荐(0)
摘要:merge sort 阅读全文
posted @ 2017-07-28 16:09 apanda009 阅读(99) 评论(0) 推荐(0)
摘要:copy 的题多用hashmap, 难点在于如何让遍历, 如何构建新的节点间的关系. An intuitive solution is to keep a hash table for each node in the list, via which we just need to iterate 阅读全文
posted @ 2017-07-23 21:28 apanda009 阅读(196) 评论(0) 推荐(0)
摘要:refer to: https://discuss.leetcode.com/topic/60394/easy-concept-with-python-c-java-solution E.g.input: [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]subar 阅读全文
posted @ 2017-07-10 23:05 apanda009 阅读(240) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/linked-list-cycle-ii/#/description 阅读全文
posted @ 2017-07-05 20:09 apanda009 阅读(141) 评论(0) 推荐(0)
摘要:https://leetcode.com/problems/partition-list/#/description http://www.cnblogs.com/EdwardLiu/p/3807137.html Given a linked list and a value x, partitio 阅读全文
posted @ 2017-07-03 17:38 apanda009 阅读(133) 评论(0) 推荐(0)
摘要:https://leetcode.com/problemset/all/?search=19 涉及链表删除操作的时候,稳妥起见都用 dummynode,省去很多麻烦。因为不一定什么时候 head 就被删了。 快慢指针 阅读全文
posted @ 2017-07-01 18:36 apanda009 阅读(136) 评论(0) 推荐(0)