public void TestRotateLinkedList() { var head = MyLinkList.BuildListNodeFromArray(new[] { 1, 2, 3, 4, 5, 6, 7 }); RotateLinkedList.Rotate(head, 5); Assert.AreEqual(head.Next.Val, 3); }
public void TestRemoveNthFromEnd() { var head = MyLinkList.BuildListNodeFromArray(new[] { 1, 2, 3, 4, 5 }); head = RemoveNthFromEnd.S1(head, 2); Assert.AreEqual(head.Length(), 4); }
//假设他们有交集,他们交集的长度必相同的,所以len1和len2的差值是len的话,较长链表的长度-len后,长度肯定还是大于等于公共部分 //所以让长的那个链表先多走len步后,每一步判定2个链表的值是否相同就行了 public static MyLinkList GetIntersection1(MyLinkList node1, MyLinkList node2) { var len1 = node1.Length(); var len2 = node2.Length(); if (len1 > len2) { while (len1 > len2) { node1 = node1.Next; len1--; } } else if (len1 < len2) { while (len1 < len2) { node2 = node2.Next; len2--; } } while (node1 != null) { if (node1 == node2) { return(node1); } node1 = node1.Next; node2 = node2.Next; } return(null); }
public void TestLinkListCycle_notcycle() { var head = MyLinkList.BuildListNodeFromArray(new[] { 1, 2, 3, 4, 5, 6, 7 }); var result = LinkListCycle.IsCycle(head); Assert.AreEqual(result, false); }
//https://www.cnblogs.com/EdwardLiu/p/3718025.html //改进就是不new 新节点 新链表指向已有节点 public static MyLinkList Merge(MyLinkList head1, MyLinkList head2) { if (head1.Next == null) { return(head2); } if (head2.Next == null) { return(head1); } var head3 = new MyLinkList(); var dummy = head3; while (head1.Next != null || head2.Next != null) { if (head1.Next != null && head2.Next != null) { if (head1.Next.Val <= head2.Next.Val) { head3.Next = new MyLinkList() { Val = head1.Next.Val }; head3 = head3.Next; head1 = head1.Next; } else { head3.Next = new MyLinkList() { Val = head2.Next.Val }; head3 = head3.Next; head2 = head2.Next; } } else if (head1.Next != null && head2.Next == null) { head3.Next = new MyLinkList() { Val = head1.Next.Val }; head3 = head3.Next; head1 = head1.Next; } else if (head1.Next == null && head2.Next != null) { head3.Next = new MyLinkList() { Val = head2.Next.Val }; head3 = head3.Next; head2 = head2.Next; } } return(dummy); }
public void TestReserverInMtoN() { var r = ReverseLinkedList.ReserverInMtoN(MyLinkList.BuildListNodeFromArray(new[] { 1, 2, 3, 4, 5, 6, 7 }), 3, 6); Assert.AreEqual(r.Next.Next.Next.Val, 6); Assert.AreEqual(r.Next.Next.Next.Next.Next.Next.Val, 3); }
public static MyLinkList Reserve_Recursive(MyLinkList head) { var newHead = Reserve_Recursive_helper(head.Next); head.Next = newHead;//这里只是因为我自己的定义的head总是dummyhead return(head); }
//每次尝试前进k个,如果能有k个元素,说明需要反转一次, //那么只要知道这k个元素的第一个元素之前的head和,最后一个元素指向的tail就能翻转其中那一节 public static MyLinkList Reserve(MyLinkList head, int k) { if (k < 2)//k=1不用翻转 { return(head); } int i = 0; var dummyHead = head; var loopHead = head; while (head.Next != null) { head = head.Next; i++; if (i == k) { //自己的思路,这里head不停往后移动,但是链表局部被翻转了,head.next跑最前面去了 //所以我知道当前loophead的next在翻转后肯定在tail之前,所以他就就是下一次looptail,循环指针也指向他 var nextloophead = loopHead.Next; ReserveList(loopHead, head.Next); i = 0; head = nextloophead; loopHead = nextloophead; } } return(dummyHead); }
//Reverse a linked list from position m to n. Do it in one-pass. //Note: 1 ≤ m ≤ n ≤ length of list. public static MyLinkList ReserverInMtoN(MyLinkList head, int m, int n) { if (head.Next == null || head.Next.Next == null) { return(head); } var count = 1; var curr = head.Next; while (count < m - 1) //前进到要翻转的节点之前,要从第四个节点开始翻转,前进到3就行了,起始是1,所以只要走2步 { count++; curr = curr.Next; } //curr下一个元素就是要开始倒置的节点 var tmphead = curr; curr = curr.Next; while (count < n - 1)//前进到最后一个要翻转的节点之前就行了,他之前那个节点的操作会把它转到最前面去 { var tmp = curr.Next; curr.Next = tmp.Next; tmp.Next = tmphead.Next; tmphead.Next = tmp; count++; } return(head); }
public static MyLinkList KeepOnly1(MyLinkList head) { if (head.Next == null || head.Next.Next == null) { return(head); } //从第二个值开始往前比较 var curr = head.Next.Next; var pre = head.Next; while (curr != null) { if (curr.Val == pre.Val) { pre.Next = curr.Next; } else { pre = curr; } curr = curr.Next; } return(head); }
//Given a linked list and a value X, partition it such that all nodes less than X come before nodes greater than or equal to X. // You should preserve the original relative order of the nodes in each of the two partitions. public static MyLinkList Do(MyLinkList head, int x) { //dummy 和head作为较小值新链表的头和结尾 var dummy = head; var curr = head.Next; //bLink和 bLinkDummy作为较大值新链表的头 var bLink = new MyLinkList(); var bLinkDummy = bLink; while (curr != null) { if (curr.Val < x) { head.Next = curr; head = head.Next; } else { bLink.Next = curr; bLink = bLink.Next; } curr = curr.Next; } //小链表的尾巴指向新链表的第一个值 head.Next = bLinkDummy.Next; return(dummy); }
public static MyLinkList Rotate(MyLinkList head, int k) { var len = head.Length(); k = k % len; if (k == 0)//无需操作 { return(head); } //末尾k次,正向就是len-k个的下个元素 var ahead = len - k + 1; var curr = head; var mid = head; for (var i = 1; i <= len; i++) { curr = curr.Next; if (i == ahead) { mid = curr; } } //此时 curr 是 end元素,mid 是翻转后的第一个元素 var first = head.Next; head.Next = mid; curr.Next = first; return(head); }
public static MyLinkList RemoveAllIfDuplicated(MyLinkList head) { if (head.Next == null || head.Next.Next == null) { return(head); } var curr = head.Next.Next; var pre = head.Next; var value = head.Next.Val; var exist = head; //上一个没被删除的节点 while (curr != null) { if (curr.Val == value) // 值重 jump { exist.Next = curr.Next; } else//不重复 { value = curr.Val; exist = pre; } pre = curr; curr = curr.Next; } return(head); }
//这道有难度吗? public static void Swap(MyLinkList head) { if (head.Next == null) { return; } var curr = head; //假设当前指针后是123 go while (curr.Next != null && curr.Next.Next != null) { var tmp2 = curr.Next.Next; var tmp1 = curr.Next; //2后面存在3 if (curr.Next.Next.Next != null) { //1指向 3,现在12都指向3 tmp1.Next = curr.Next.Next.Next; } else { tmp1.Next = null; } //头指向2 curr.Next = tmp2; //2指向1 tmp2.Next = tmp1; curr = curr.Next.Next; } }
public void TestRemoveLinkedListElements() { var list = MyLinkList.BuildListNodeFromArray(new[] { 9, 1, 2, 9, 9, 3, 9, 4, 5, 6, 7, 8, 9 }); var r = RemoveLinkedListElements.Remove(list, 9); Assert.AreEqual(r.Length(), 8); }
public static MyLinkList Sort(MyLinkList head) { //一个新链表的头 var dummy = new MyLinkList { Val = -1 }; //当前链表循环的头 var curr = head.Next; while (curr != null) { //每次都从新链表的头开始循环,每次开始的时候新链表都是排序的(第一次只有一个元素的时候不用排序) //新链表循环的标志 var pre = dummy; //新链表都是排序过的,而且每个值都需要比较,停留的位置,就是curr需要在之后插入的位置 while (pre.Next != null && pre.Next.Val < curr.Val) { pre = pre.Next; } var tmp = curr.Next; // 把curr插到pre之后,所以其实这时候有两个链表了,老链表curr在后移,新链表在增加,而且不用考虑是不是插入在最后或者中间 curr.Next = pre.Next; pre.Next = curr; curr = tmp; } return(dummy); }
public void TestPartitionList() { var head = MyLinkList.BuildListNodeFromArray(new[] { 1, 4, 3, 2, 5, 2 }); head = PartitionList.Do(head, 3); Assert.AreEqual(head.Next.Next.Val, 2); Assert.AreEqual(head.Next.Next.Next.Next.Val, 4); }
public void TestRemoveDuplicatedFromLinkedList2() { var head = MyLinkList.BuildListNodeFromArray(new[] { 1, 1, 2, 3, 3, 4, 5, 5 }); var r = RemoveDuplicatesFromSortedList.RemoveAllIfDuplicated(head); Assert.AreEqual(r.Next.Next.Val, 4); Assert.AreEqual(r.Length(), 2); }
public void TestMergeTwoSortedList() { var head1 = MyLinkList.BuildListNodeFromArray(new[] { 1, 2, 4 }); var head2 = MyLinkList.BuildListNodeFromArray(new[] { 1, 3, 4 }); var head3 = MergeTwoSortedList.Merge(head1, head2); Assert.AreEqual(head3.Length(), 6); }
public void TestReverseNodesinkGroup() { var head1 = MyLinkList.BuildListNodeFromArray(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }); var r = ReverseNodesinkGroup.Reserve(head1, 3); Assert.AreEqual(r.Next.Next.Next.Val, 1); Assert.AreEqual(r.Next.Next.Next.Next.Next.Next.Next.Next.Next.Next.Next.Val, 11); }
public void AddTest() { var list = new MyLinkList(); list.Add("A"); list.Add("B"); list.Add("C"); Assert.AreEqual("ABC", list.ToString()); }
public void IsContainTestFalse() { var list = new MyLinkList(); list.Add("A"); list.Add("B"); list.Add("C"); Assert.AreEqual(false, list.IsContain("D")); }
public void AddAtStartTest() { var list = new MyLinkList(); list.AddAtStart("A"); list.AddAtStart("B"); list.AddAtStart("C"); Assert.AreEqual("CBA", list.ToString()); }
public void TestConvertSortedListToBST() { var root = ConstructTree.ConvertSortedListToBST(MyLinkList.BuildListNodeFromArray(new [] { -10, -3, 0, 5, 9 })); //因为可以有多个结果可能,所以只是调用了isvalid方法验证是否是bst 和root不为null var r = ValidateBinarySearchTree.IsValid(root); Assert.AreEqual(r, true); Assert.IsNotNull(root); }
public void TestRecordList() { var head = MyLinkList.BuildListNodeFromArray(new[] { 1, 2, 3, 4, 5, 6, 7 }); RecordList.Record(head); Assert.AreEqual(head.Next.Next.Val, 7); Assert.AreEqual(head.Next.Next.Next.Next.Val, 6); Assert.AreEqual(head.Next.Next.Next.Next.Next.Next.Val, 5); Assert.AreEqual(head.Next.Next.Next.Next.Next.Next.Next.Val, 4); }
public void RemoveAllTest() { var list = new MyLinkList(); list.Add("A"); list.Add("B"); list.Add("C"); list.RemoveAll(); Assert.AreEqual("", list.ToString()); }
public void TestInsertionSortList() { var head = MyLinkList.BuildListNodeFromArray(new[] { 3, 5, 2, 6, 7, 4, 1 }); var dummy = InsertionSortList.Sort(head); Assert.AreEqual(dummy.Next.Val, 1); Assert.AreEqual(dummy.Next.Next.Next.Val, 3); Assert.AreEqual(dummy.Next.Next.Next.Next.Next.Val, 5); Assert.AreEqual(dummy.Next.Next.Next.Next.Next.Next.Next.Val, 7); }
//快慢指针的方式感觉好麻烦 //list先转成arry,不就和上面一题一样 了 public static TreeNode ConvertSortedListToBST(MyLinkList linkList) { var list = new List<int>(); var curr = linkList.Next; while (curr != null) { list.Add(curr.Val); curr = curr.Next; } var arr = list.ToArray(); return ConvertSortedArrayToBST_helper(arr, 0, arr.Length - 1); }
/// <summary> /// </summary> /// <param name="head"></param> /// <param name="tail"></param> private static void ReserveList(MyLinkList head, MyLinkList tail) { //其实就是curr.next指向了 next.next var curr = head.Next; while (curr.Next != tail) { var tmp = curr.Next; curr.Next = tmp.Next; tmp.Next = head.Next; head.Next = tmp; } }
public void TestReverseLinkedList() { var r = ReverseLinkedList.Reserve(MyLinkList.BuildListNodeFromArray(new[] { 1, 2, 3, 4, 5 })); Assert.AreEqual(r.Next.Val, 5); Assert.AreEqual(r.Next.Next.Val, 4); Assert.AreEqual(r.Next.Next.Next.Next.Next.Val, 1); r = ReverseLinkedList.Reserve_Recursive(MyLinkList.BuildListNodeFromArray(new[] { 1, 2, 3, 4, 5 })); Assert.AreEqual(r.Next.Val, 5); Assert.AreEqual(r.Next.Next.Val, 4); Assert.AreEqual(r.Next.Next.Next.Next.Next.Val, 1); }