随笔分类 -  leetcode-linkedlList

摘要:1 public static boolean(ListNode l1,ListNode l2){ 2 boolean res = false; 3 int [] check = {-1}; 4 ListNode mid = getMid(head,check); 5 if( check[0]==1){ 6 ListNode next = mid.next; 7 ListNode newHead =reverse(next); 8 res=checkPalidrom(head, newHead ); 9 newHead =re... 阅读全文
posted @ 2014-02-26 02:47 krunning
摘要:1 public class Solution { 2 public ListNode insertionSortList(ListNode head) { 3 if(head==null) return head; 4 ListNode safe = new ListNode(Integer.MIN_VALUE); 5 ListNode p2 = head; 6 while(p2!=null){ 7 ListNode pre = find(safe,p2); 8 List... 阅读全文
posted @ 2014-02-24 04:53 krunning
摘要:1 public class Solution { 2 public ListNode sortList(ListNode head) { 3 if(head==null || head.next==null) return head; 4 ListNode fast = head, slow = head; 5 while(true){ 6 fast = fast.next; 7 if(fast==null) break; 8 fast = fast.next; ... 阅读全文
posted @ 2014-02-24 04:50 krunning
摘要: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 example,Given{1,2,3,4}, reorder it to{1,4,2,3}. 1 public class Solution { 2 public void reorderList(ListNode head) { 3 if(head==null) return; ... 阅读全文
posted @ 2014-02-22 16:23 krunning
摘要:1 public class LRUCache { 2 HashMap ht; 3 int curSize; 4 int size; 5 entry first=null; 6 entry last=null; 7 public LRUCache(int capacity) { 8 size = capacity; 9 ht = new HashMap();10 curSize = 0;11 }12 13 public int get(int key) {14 ... 阅读全文
posted @ 2014-02-22 16:20 krunning
摘要:1 public class Solution { 2 public ListNode detectCycle(ListNode head) { 3 if(head==null) return head; 4 ListNode fast = head; 5 ListNode slow = head; 6 while(fast!=null){ 7 slow = slow.next; 8 fast = fast.next; 9 if(fast!=null... 阅读全文
posted @ 2014-02-22 13:09 krunning
摘要:Given a linked list, determine if it has a cycle in it. 1 public class Solution { 2 public boolean hasCycle(ListNode head) { 3 if(head==null) return false; 4 ListNode fast = head; 5 ListNode slow = head; 6 while(fast!=null){ 7 slow = slow.next; 8 ... 阅读全文
posted @ 2014-02-22 13:08 krunning
摘要: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. 1 public class Solution { 2 public RandomListNode copyRandomList(RandomListNode head) { 3 if(head==null) return head; 4 //insert 5 RandomLis... 阅读全文
posted @ 2014-02-22 13:04 krunning
摘要: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. 1 public class Solution { 2 public ListNode rotateRight(ListNode head, int n) { 3 if(head==null || n==0) return head; 4 int 阅读全文
posted @ 2014-02-17 05:50 krunning
摘要: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. 1 public class Solution { 2 public ListNode reverseBe 阅读全文
posted @ 2014-02-17 02:14 krunning
摘要: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- 阅读全文
posted @ 2014-02-10 15:31 krunning
摘要: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. 1 public class Solution { 2 public ListNode deleteDu 阅读全文
posted @ 2014-02-06 14:45 krunning
摘要:Given a sorted linked list, delete all duplicates such that each element appear onlyonce. 1 public class Solution { 2 public ListNode deleteDuplicates(ListNode head) { 3 ListNode cur =head; 4 while(cur!=null&&cur.next!=null){ 5 if(cur.val==cur.next.val){ 6 ... 阅读全文
posted @ 2014-02-06 14:42 krunning
摘要:1 public class Solution { 2 public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 3 ListNode safe = new ListNode(-1); 4 ListNode pre = safe; 5 while(l1!=null && l2!=null){ 6 if(l1.val<l2.val){ 7 pre.next = l1; 8 l1 = l1.nex... 阅读全文
posted @ 2014-02-06 14:31 krunning
摘要: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 阅读全文
posted @ 2014-02-06 13:59 krunning
摘要:Given 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. 1 public class Solut 阅读全文
posted @ 2014-02-06 05:39 krunning
摘要:1 /* Min heap*/ 2 public class Solution { 3 public ListNode mergeKLists(ArrayList lists) { 4 if(lists.size() comparator = new Comparator(){ 7 public int compare(ListNode m,ListNode n){ 8 return m.val-n.val; 9 }10 };11 PriorityQueue... 阅读全文
posted @ 2014-02-06 05:37 krunning
摘要:Given a linked list, remove thenthnode from the end of list and return its head. 1 public class Solution { 2 public ListNode removeNthFromEnd(ListNode head, int n) { 3 ListNode safe = new ListNode(-1); 4 safe.next = head; 5 ListNode p1 = safe; 6 for(int i=0;i<=n;i... 阅读全文
posted @ 2014-02-06 05:33 krunning