public ListNodeClass MergeKLists(ListNodeClass[] lists) { ListNodeClass head = new ListNodeClass(0); ListNodeClass p = head; ListNodeClass pMin = null; bool end = false; int length = lists.Length; int index = 0; while (!end) { pMin = new ListNodeClass(int.MaxValue); end = true; for (int i = 0; i < length; i++) { if (lists[i] != null && pMin.val >= lists[i].val) { pMin = lists[i]; index = i; end = false; } } if (!end) { p.next = pMin; p = p.next; lists[index] = lists[index].next; } } return(head.next); }
private ListNodeClass Reverse(ListNodeClass p, int k) { int counter = 0; ListNodeClass[] tmps = new ListNodeClass[k]; ListNodeClass pHead = p; while (p != null) { tmps[counter] = p; counter++; p = p.next; if (counter == k) { break; } } if (counter < k) { return(pHead); } for (int i = k - 1; i > 0; i--) { tmps[i].next = tmps[i - 1]; } tmps[0].next = Reverse(p, k); return(tmps[k - 1]); }
/// <summary> /// 给定一个链表,返回一个list,使得所有小于x的节点,在大于等于x的节点的左边 /// </summary> /// <param name="head"></param> /// <param name="x"></param> /// <returns></returns> public ListNodeClass Partition(ListNodeClass head, int x) { ListNodeClass headL = new ListNodeClass(0); ListNodeClass headGE = new ListNodeClass(0); ListNodeClass pL = headL; ListNodeClass pGE = headGE; if (head == null || head.next == null) { return(head); } while (head != null) { if (head.val < x) { pL.next = head; pL = pL.next; } else { pGE.next = head; pGE = pGE.next; } head = head.next; } pL.next = headGE.next; pGE.next = null; return(headL.next); }
/// <summary> /// 给定一个升序链表,返回它所对应的深度平衡BST /// </summary> /// <param name="head"></param> /// <returns></returns> public TreeNode SortedListToBST(ListNodeClass head) { TreeNode res = null; //快慢指针:一个指针跳一格,另一个指针跳两格,选取中间项,时间复杂度为n^2,空间复杂度为1 //考虑遍历两遍,形成一个数组方便随机访问,时间复杂度为n,空间复杂度为n if (head == null) { return(null); } int counter = 0; ListNodeClass p = head; while (p != null) { counter++; p = p.next; } int[] nums = new int[counter]; counter = 0; p = head; while (p != null) { nums[counter++] = p.val; p = p.next; } res = DoRecursionBySortedArrayToBST(nums, 0, counter - 1); return(res); }
public ListNodeClass ReverseKGroup(ListNodeClass head, int k) { if (head == null) { return(null); } head = Reverse(head, k); return(head); }
private ListNodeClass MergeTwoLists2(ListNodeClass l1, ListNodeClass l2) { if (l1 == null && l2 == null) { return(null); } if (l1 == null && l2 != null) { return(l2); } if (l1 != null && l2 == null) { return(l1); } var head = new ListNodeClass(0); var temp = head; var p1 = l1; var p2 = l2; while (true) { if (p1 == null) { temp.next = p2; break; } if (p2 == null) { temp.next = p1; break; } if (p1.val < p2.val) { temp.next = p1; temp = temp.next; p1 = p1.next; } else { temp.next = p2; temp = temp.next; p2 = p2.next; } } return(head.next); }
public ListNodeClass RemoveNthFromEnd(ListNodeClass head, int n) { ListNodeClass p = head; ListNodeClass pTarget = null; ListNodeClass pPre = null; int counter = 0; while (p.next != null) { counter++; if (counter == n + 1) { pPre = head; } if (counter == n) { pTarget = head; } if (pPre != null) { pPre = pPre.next; } if (pTarget != null) { pTarget = pTarget.next; } p = p.next; } if (pPre != null && pTarget != null) { pPre.next = pTarget.next; } else if (pPre == null && pTarget != null) { head.next = head.next.next; } else if (pPre == null && pTarget == null && counter == 0) { return(null); } else if (pPre == null && pTarget == null) { head = head.next; } return(head); }
public ListNodeClass MergeTwoLists(ListNodeClass l1, ListNodeClass l2) { if (l1 == null && l2 != null) { return(l2); } else if (l1 != null && l2 == null) { return(l1); } else if (l1 == null && l2 == null) { return(null); } ListNodeClass head = null; ListNodeClass p = null; head = l1.val <= l2.val ? l1 : l2; l1 = l1 == head ? l1.next : l1; l2 = l2 == head ? l2.next : l2; p = head; while (l1 != null && l2 != null) { if (l1.val <= l2.val) { p.next = l1; l1 = l1.next; } else { p.next = l2; l2 = l2.next; } p = p.next; } if (l2 == null) { p.next = l1; } else if (l1 == null) { p.next = l2; } return(head); }
/// <summary> /// 给定一个链表和两个数字,翻转介于这两个数字之间的数值 /// 申明两个头结点用于存储传入的head和逆向拼接的head,为了应对头转换 /// 存储p1位起始位置前一个元素,p2为逆向拼接的尾元素,最后拼接p1、逆向链表和结束位置的迭代p即可 /// </summary> /// <param name="head"></param> /// <param name="m"></param> /// <param name="n"></param> /// <returns></returns> public ListNodeClass ReverseBetween(ListNodeClass head, int m, int n) { if (head == null) { return(head); } int counter = 0; ListNodeClass headtmp = new ListNodeClass(0); headtmp.next = head; head = headtmp; m++; n++; ListNodeClass p = head; ListNodeClass head2 = new ListNodeClass(0); ListNodeClass p1 = null; ListNodeClass p2 = null; ListNodeClass tmp = null; while (counter < n && p != null) { counter++; if (counter == m - 1) { p1 = p; } if (counter == m) { p2 = p; } if (counter >= m) { tmp = head2.next; head2.next = p; p = p.next; head2.next.next = tmp; continue; } p = p.next; } p1.next = head2.next; p2.next = p; return(head.next); }
public ListNodeClass SwapPairs(ListNodeClass head) { ListNodeClass resHead = null; ListNodeClass p1 = null; ListNodeClass p2 = null; ListNodeClass p = null; //for the first p1 = head; if (p1 == null) { return(null); } p2 = p1.next; if (p2 == null) { return(head); } resHead = p2; p1.next = p2.next; p2.next = p1; p = p1; //for loop while (true) { if (p1.next == null) { break; } p1 = p1.next; if (p1.next == null) { break; } p2 = p1.next; p1.next = p2.next; p2.next = p1; p.next = p2; p = p1; } return(resHead); }
//almost the same speed as mine public ListNodeClass BestSolution(ListNodeClass head) { if (head != null && head.next != null) { ListNodeClass nextHead = head.next.next; var temp = head; head = head.next; head.next = temp; if (nextHead != null) { temp.next = SwapPairs(nextHead); } else { temp.next = null; } } return(head); }
public ListNodeClass RotateRight(ListNodeClass head, int k) { if (head == null || head.next == null) { return(head); } int counter = 1; ListNodeClass p = head; ListNodeClass tail = null; ListNodeClass tail_pre = null; p = head; while (p.next != null) { tail_pre = p; p = p.next; counter++; } counter = k % counter; for (int i = 0; i < counter; i++) { tail = p; tail.next = head; head = tail; tail_pre.next = null; if (i == counter - 1) { break; } else { p = head; while (p.next != null) { tail_pre = p; p = p.next; } } } return(head); }