public ListNode Partition(ListNode head, int x) { ListNode bp = new ListNode(0); ListNode bpp = bp; ListNode sp = new ListNode(0); ListNode spp = sp; ListNode ptr = head; while(ptr != null) { if (ptr.val < x) { bpp.next = ptr; bpp = ptr; } else { spp.next = ptr; spp = ptr; } ptr = ptr.next; } bpp.next = sp.next; spp.next = null; return bp.next; }
public ListNode OddEvenList(ListNode head) { if (head == null || head.next == null) return head; ListNode odd = head; ListNode even = head.next; ListNode ptr = odd; ListNode eptr = even; while(ptr != null && eptr != null) { ptr.next = eptr.next; if (ptr.next == null) break; ptr = ptr.next; eptr.next = ptr.next; eptr = eptr.next; } ptr.next = even; return odd; }
public ListNode RemoveNthFromEnd(ListNode head, int n) { ListNode ptr = head; int count = 0; if (head == null) return null; while (ptr != null) { ptr = ptr.next; count++; } count -= n; if (count == 0) return head.next; ptr = head; while(count>1) { ptr = ptr.next; count--; } ptr.next = ptr.next.next; return head; }
public ListNode ReverseList(ListNode head) { if (head == null || head.next == null) return head; var p = head; var q = head.next; head.next = null; while (true) { if (q.next == null) { q.next = p; break; } else { // p q->r var r = q.next; // p<-q r q.next = p; p = q; q = r; } } return q; }
public ListNode GetIntersectionNode(ListNode headA, ListNode headB) { ListNode ptrA = headA; ListNode ptrB = headB; bool flagA = true; bool flagB = true; if (headA == null || headB == null) return null; while (ptrA != null && ptrB != null) { if (ptrA == ptrB) return ptrA; ptrA = ptrA.next; ptrB = ptrB.next; if (ptrA == null && flagA) { ptrA = headB; flagA = false; } if (ptrB == null && flagB) { ptrB = headA; flagB = false; } } return null; }
public ListNode RotateRight(ListNode head, int k) { ListNode tail = head; int len = 1; if (k == 0 || head == null) return head; while (tail.next != null) { len++; tail = tail.next; } k = len - k % len; if (k == len) return head; tail.next = head; len = 1; tail = head; while(len < k) { tail = tail.next; len++; } head = tail.next; tail.next = null; return head; }
public void ReorderList(ListNode head) { if (head == null || head.next == null) return; ListNode p = head; ListNode pre = p; ListNode ptr = head; while(ptr != null && ptr.next != null) { pre = p; p = p.next; ptr = ptr.next.next; } pre.next = null; p = ReverseList(p); ptr = p; pre = head; while (p != null && head != null) { ptr = p.next; pre = head.next; head.next = p; if (pre != null) p.next = pre; p = ptr; head = pre; } }
public ListNode SortList(ListNode head) { if (head == null || head.next == null) return head; bool flag = true; ListNode lefthead = head; ListNode righthead = head.next; ListNode left = lefthead; ListNode right = righthead; head = head.next.next; while(head != null) { if (flag) { left.next = head; left = left.next; } else { right.next = head; right = right.next; } head = head.next; flag = !flag; } left.next = null; right.next = null; left = SortList(lefthead); right = SortList(righthead); return merge(left, right); }
public ListNode MergeKLists(ListNode[] lists) { int len = lists.Length; if (len == 0) return null; while(len > 1) { int j = 0; int pos = 0; while(j+1 < len) { lists[pos] = MergeTwoLists(lists[j], lists[j + 1]); j += 2; pos++; } if (j<len) { lists[pos] = lists[j]; pos++; } len = pos; } return lists[0]; }
public ListNode MergeTwoLists(ListNode l1, ListNode l2) { ListNode head = new ListNode(-1); ListNode cur = head; if (l1 == null) return l2; else if (l2 == null) return l1; while(l1 != null && l2 != null) { if (l1.val <= l2.val) { cur.next = l1; cur = cur.next; l1 = l1.next; } else { cur.next = l2; cur = cur.next; l2 = l2.next; } } if (l1 == null && l2 != null) cur.next = l2; else if (l1 != null && l2 == null) cur.next = l1; return head.next; }
public TreeNode SortedListToBST(ListNode head) { if (head == null) return null; if (head.next == null) return new TreeNode(head.val); ListNode p = head; ListNode q = head; ListNode pre = q; while(p != null && p.next != null) { pre = q; q = q.next; p = p.next.next; } TreeNode tn = new TreeNode(q.val); if (q == head) { tn.right = new TreeNode(q.next.val); } else { tn = new TreeNode(q.val); pre.next = null; tn.left = SortedListToBST(head); tn.right = SortedListToBST(q.next); } return tn; }
public ListNode DetectCycle(ListNode head) { if (head == null || head.next == null) return null; ListNode p = head; ListNode p2 = head.next; while (p != null && p2 != null && p != p2) { p = p.next; if (p2.next == null) return null; p2 = p2.next.next; } if (p != p2) return null; p2 = head; while(p2 != p) { p = p.next; p2 = p2.next; } return p2; }
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) { int flag = 0; ListNode head = new ListNode(0); ListNode tmp = null; var re = head; while (l1 != null && l2 != null) { int val = l1.val + l2.val + flag; if (val >= 10) { flag = 1; val -= 10; } else flag = 0; re.next = new ListNode(val); re = re.next; l1 = l1.next; l2 = l2.next; } if (l1 != null) tmp = l1; else if (l2 != null) tmp = l2; while (tmp != null) { var val = tmp.val + flag; if (val >= 10) { flag = 1; val -= 10; } else flag = 0; re.next = new ListNode(val); re = re.next; tmp = tmp.next; } if (flag == 1) { re.next = new ListNode(1); re.next.next = null; } else re.next = null; return head.next; }
public void DeleteNode(ListNode node) { if (node.next == null) return; ListNode pre = null; while(node.next != null) { node.val = node.next.val; pre = node; node = node.next; } pre.next = null; }
public static void Run() { var st = new Solution61(); Console.WriteLine("Start: {0}", DateTime.Now); ListNode p = new ListNode(1); p.next = new ListNode(2); p.next.next = new ListNode(3); p.next.next.next = new ListNode(4); var re = st.RotateRight(p, 4); Util.PrintListNode(re); Console.WriteLine("End: {0}", DateTime.Now); }
public ListNode SwapPairs(ListNode head) { ListNode re = head; if (head == null || head.next == null) return head; re = head.next; head.next = re.next; re.next = head; head.next = SwapPairs(head.next); return re; }
public static void Run() { var st = new Solution83(); Console.WriteLine("Start: {0}", DateTime.Now); ListNode p = new ListNode(1); p.next = new ListNode(1); p.next.next = new ListNode(2); p.next.next.next = new ListNode(4); p.next.next.next.next = null; Util.PrintListNode(st.DeleteDuplicates(p)); Console.WriteLine("End: {0}", DateTime.Now); }
public static void Run() { var st = new Solution19(); Console.WriteLine("Start: {0}", DateTime.Now); ListNode p = new ListNode(1); p.next = new ListNode(2); p.next.next = new ListNode(3); p.next.next.next = new ListNode(4); p.next.next.next.next = null; Util.PrintListNode(st.RemoveNthFromEnd(p, 3)); Util.PrintListNode(st.RemoveNthFromEnd(p, 2)); Console.WriteLine("End: {0}", DateTime.Now); }
public static void PrintListNode(ListNode p) { if (p == null) Console.WriteLine("<null>"); else while (true) { Console.Write("{0} -> ", p.val); if (p.next == null) { Console.WriteLine("<end>"); break; } else p = p.next; } }
public static void Run() { var st = new Solution234(); ListNode lt = new ListNode(1); lt.next = new ListNode(2); lt.next.next = new ListNode(3); lt.next.next.next = new ListNode(2); lt.next.next.next.next = new ListNode(1); lt.next.next.next.next.next = null; Console.WriteLine("Start: {0}", DateTime.Now); var re = st.IsPalindrome(lt); Console.WriteLine("{0}", re); Console.WriteLine("End: {0}", DateTime.Now); }
public static void Run() { var st = new Solution143(); Console.WriteLine("Start: {0}", DateTime.Now); ListNode p = new ListNode(1); p.next = new ListNode(2); p.next.next = new ListNode(3); p.next.next.next = new ListNode(4); p.next.next.next.next = new ListNode(5); st.ReorderList(p); Util.PrintListNode(p); Console.WriteLine("End: {0}", DateTime.Now); }
public static void Run() { var st = new Solution86(); Console.WriteLine("Start: {0}", DateTime.Now); ListNode p = new ListNode(1); p.next = new ListNode(4); p.next.next = new ListNode(3); p.next.next.next = new ListNode(2); p.next.next.next.next = new ListNode(5); p.next.next.next.next.next = new ListNode(2); Util.PrintListNode(st.Partition(p, 3)); Console.WriteLine("End: {0}", DateTime.Now); }
public static void Run() { var st = new Solution21(); Console.WriteLine("Start: {0}", DateTime.Now); ListNode p = new ListNode(1); p.next = new ListNode(3); p.next.next = null; ListNode q = new ListNode(2); q.next = new ListNode(4); q.next.next = null; Util.PrintListNode(st.MergeTwoLists(p, q)); Console.WriteLine("End: {0}", DateTime.Now); }
public static void Run() { var st = new Solution24(); ListNode p = new ListNode(1); p.next = new ListNode(2); p.next.next = new ListNode(3); p.next.next.next = new ListNode(4); p.next.next.next.next = null; Console.WriteLine("Start: {0}", DateTime.Now); var re = st.SwapPairs(p); Util.PrintListNode(re); Console.WriteLine("End: {0}", DateTime.Now); }
public ListNode RemoveElements(ListNode head, int val) { if (head == null) return head; ListNode fhead = new ListNode(0); fhead.next = head; ListNode ptr = fhead; while (ptr.next != null) { if (ptr.next.val == val) ptr.next = ptr.next.next; else ptr = ptr.next; } return fhead.next; }
public static void Run() { var st = new Solution328(); Console.WriteLine("Start: {0}", DateTime.Now); ListNode p = new ListNode(1); p.next = new ListNode(2); p.next.next = new ListNode(3); p.next.next.next = new ListNode(4); p.next.next.next.next = new ListNode(5); //p.next.next.next.next.next = new ListNode(6); Util.PrintListNode(st.OddEvenList(p)); Console.WriteLine("End: {0}", DateTime.Now); }
public static void Run() { var st = new Solution147(); Console.WriteLine("Start: {0}", DateTime.Now); ListNode p = new ListNode(5); p.next = new ListNode(4); p.next.next = new ListNode(6); p.next.next.next = new ListNode(7); p.next.next.next.next = new ListNode(8); p = st.InsertionSortList(p); Util.PrintListNode(p); Console.WriteLine("End: {0}", DateTime.Now); }
public static void Run() { var st = new Solution25(); Console.WriteLine("Start: {0}", DateTime.Now); ListNode p1 = new ListNode(1); p1.next = new ListNode(2); p1.next.next = new ListNode(3); p1.next.next.next = new ListNode(4); //p1.next.next.next.next = new ListNode(5); var re = st.ReverseKGroup(p1, 2); Util.PrintListNode(re); Console.WriteLine("End: {0}", DateTime.Now); }
public static void Run() { var st = new Solution206(); Console.WriteLine("Start: {0}", DateTime.Now); ListNode p = new ListNode(3); p.next = new ListNode(4); p.next.next = new ListNode(5); p.next.next.next = null; Util.PrintListNode(st.ReverseList(p)); var q = new ListNode(0); q.next = null; Util.PrintListNode(st.ReverseList(q)); Console.WriteLine("End: {0}", DateTime.Now); }
public static void Run() { var st = new Solution237(); Console.WriteLine("Start: {0}", DateTime.Now); ListNode p = new ListNode(1); p.next = new ListNode(2); ListNode q = new ListNode(3); q.next = new ListNode(4); q.next.next = null; p.next.next = q; st.DeleteNode(q); Util.PrintListNode(p); Console.WriteLine("End: {0}", DateTime.Now); }