随笔分类 - LinkedList
摘要:Sort a linked list inO(nlogn) time using constant space complexity.Merge sort先根据mid node把list 分割为 单个 node ,然后merge/** * Definition for singly-linked l...
阅读全文
摘要:Sort a linked list using insertion sort.思路: 建立一个fake head, 依次从list中取出一个node,插入到 fake head 的list中去,从小到大排列。public class Solution { public ListNode insertionSortList(ListNode head) { ListNode prehead = new ListNode(Integer.MIN_VALUE); ListNode runner = head; while(runner != null...
阅读全文
摘要:Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the valu...
阅读全文
摘要:Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For exam...
阅读全文
摘要: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.Ref:http://fisherlei.blogspot.com/2013/11/leetcode-copy-list-with-random-pointer.html如图分三步:http://lh6.ggpht.com/-xlfWkNgeNhI/Uomwl3lB47I/A
阅读全文
摘要:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?解题思路:双指针, 一个指针一次走一步,一个指针一次走两步, 如果有环,定相遇。然后将快指针移到头部,再一次走一步,当两只真再次相遇的时候就是指针的头/** * Definition for singly-linked list. * class ListNode { * int val; * ...
阅读全文
摘要:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?两个指针,一个一次走两步,一个一次走一步,相遇说明有环/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * ...
阅读全文
摘要:Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.Note:Givenm,nsatisfy the following condition:1 ≤m≤n≤ length of list.ref:http://www.cnblogs.com/feiling/p/3263501.html添加一个s
阅读全文
摘要:Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL.ref:http://fisherlei.blogspot.com/2013/01/leetcode-rotate-list.html首先从head开始跑,直到最后一个节点,这时可以得出链表长度len。然后将尾指针指向头指针,将整个圈连起来,接着往前
阅读全文
摘要:Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given1->4->3->2->5->2andx= 3,return1->2->2->4->3-
阅读全文
摘要:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.For example,Given1->2->3->3->4->4->5, return1->2->5.Given1->1->1->2->3, return2->3.解题思路 : 建立 fake noed 加在head 前面。cur = head; 比较cur 和 cur
阅读全文
摘要:Given a linked list, reverse the nodes of a linked listkat a time and return its modified list.If the number of nodes is not a multiple ofkthen left-out nodes in the end should remain as it is.You may not alter the values in the nodes, only nodes itself may be changed.Only constant memory is allowed
阅读全文
摘要:Given a linked list, remove thenthnode from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.Note:Givennwill always be valid.Try to do this in one pass. 1
阅读全文
摘要:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.Ref:http://www.cnblogs.com/feiling/p/3267...
阅读全文
摘要:Swap Nodes in PairsGiven a linked list, swap every two adjacent nodes and return its head.For example,Given1->2->3->4, you should return the list as2->1->4->3.Your algorithm should use only constant space. You maynotmodify the values in the list, only nodes itself can be changed./*
阅读全文
摘要:Solution andPrecautions:Very similar to the classical merger sort for two sorted arrays, since the k linked list are already sorted, we just find the smallest number among the heads list[i][0] (i=0,…k-1), of course, if some list already reached to the end, that is list[i][0] is NULL, we just ignore
阅读全文
摘要:/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if(l1 == null) return l2;...
阅读全文
摘要:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single ...
阅读全文
摘要:这里使用了额外的存储空间,可以使用双指针来实现,不需额外的存储空间./** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode deleteDuplicates(ListNode head) { ...
阅读全文

浙公网安备 33010602011771号