public TreeNode rSortedListToBST(ListNode head, int size, out ListNode tail) { if (size == 1) { tail = head.next; return new TreeNode(head.val); } else { int left = size / 2; int right = size - 1 - left; var ltree = rSortedListToBST(head, left, out tail); var root = new TreeNode(tail.val); root.left = ltree; if (right > 0) { var rtree = rSortedListToBST (tail.next, right, out tail); root.right = rtree; } else { tail = tail.next; } return root; } }
public ListNode DetectCycle(ListNode head) { ListNode slow = head; ListNode fast = head; bool found = false; while(fast != null && fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; if (fast == slow) { found = true; break; } } if (found == false) { return null; } else { int len = 1; while(fast.next != slow) { len++; fast = fast.next; } if (len == 1) { return slow; } else { for(fast = head; len > 0; len--) { fast = fast.next; } slow = head; while(fast != slow) { fast = fast.next; slow = slow.next; } return slow; } } }
public void TestMergeSort() { MergeSort ms = new MergeSort (); var input = new ListNode[] { null, LinkList.ArrayToList (new int[]{-1, 5, 11}), null, LinkList.ArrayToList (new int[]{6, 10}) }; var res = ms.MergeKLists (input); }
public static ListNode ArrayToList(int[] input) { if (input == null || input.Length == 0) { return null; } ListNode head = new ListNode (input [0]); var cur = head; for (int i = 1; i < input.Length; i++) { cur.next = new ListNode (input [i]); cur = cur.next; } cur.next = null; return head; }
public ListNode MergeKLists(ListNode[] lists) { ListNode[] cur = new ListNode[lists.Length]; for(int i = 0; i < lists.Length; i++) { cur[i] = lists[i]; } ListNode head = new ListNode(0); ListNode curr = head; while(true) { var min = int.MaxValue; var minPos = -1; for(int i = 0; i < lists.Length; i++) { if (cur[i] != null && cur[i].val < min) { min = cur[i].val; minPos = i; } } if (minPos < 0) { break; } else { curr.next = cur[minPos]; curr = curr.next; cur[minPos] = cur[minPos].next; } } return head.next; }
public TreeNode SortedListToBST(ListNode head) { if (head == null) { return null; } else if (head.next == null) { return new TreeNode(head.val); } else { int size = 0; for(var cur = head; cur != null; cur = cur.next) { size++; } ListNode tail = null; return rSortedListToBST(head, size, out tail); } }
public TreeNode SortedListToBST(ListNode head) { if (head == null) { return(null); } else if (head.next == null) { return(new TreeNode(head.val)); } else { int size = 0; for (var cur = head; cur != null; cur = cur.next) { size++; } ListNode tail = null; return(rSortedListToBST(head, size, out tail)); } }
public ListNode FindKthNode(ListNode head, int k) { var cur = head; int i = 0; for(; i < k; i++) { if (cur != null) { cur = cur.next; } else { break; } } if (cur != null || i == k) { var newHead = Reverse(head, cur); head.next = cur; return newHead; } else { return head; } }
public ListNode(int x) { val = x; next = null; }
public ListNode ReverseKGroup(ListNode head, int k) { if (head == null || head.next == null || k <= 1) { return head; } ListNode newHead = new ListNode(0); ListNode prev = newHead; ListNode next = head; ListNode res = null; while(true) { res = FindKthNode(next, k); prev.next = res; prev = next; if (res == next) { break; } else { next = next.next; } } return newHead.next; }
public ListNode ReverseBetween(ListNode head, int m, int n) { var cur = head; ListNode prev = null; ListNode last = null; ListNode tail = null; for(int i = 1; i <= n; i++) { if (i < m) { prev = cur; cur = cur.next; } else if (i >= m) { if (i == m) { last = prev; tail = cur; } var temp = cur.next; cur.next = prev; prev = cur; cur = temp; } } tail.next = cur; if (last == null) { return prev; } else { last.next = prev; return head; } }
public ListNode Reverse(ListNode head, ListNode end) { ListNode prev = null; ListNode cur = head; ListNode next = cur.next; while(cur != end) { next = cur.next; cur.next = prev; prev = cur; cur = next; } return prev; }