void PushNode(SingleListNode <T> node) { // 断开关系链,避免内存泄漏 node.Item = default(T); //// 如果自由节点太多,就不要了 //if (FreeCount > MaxFreeCount) return; var newTop = node; //SpinWait sw = null; while (true) { // 记住当前 var oldTop = FreeTop; // 设置新对象的下一个节点为当前栈顶 newTop.Next = oldTop; if (Interlocked.CompareExchange(ref FreeTop, newTop, oldTop) == oldTop) { break; } Thread.SpinWait(1); //if (sw == null) sw = new SpinWait(); //sw.SpinOnce(); } Interlocked.Increment(ref FreeCount); }
public void CreateSingleListedListByArrayTest_2() { int[] nums = { 11 }; SingleListNode list = LinkedListHelper.CreateSingleListedListByArray(nums); Assert.IsTrue(true); }
/// <summary> /// 1.用双指针 做快慢指针 找中间节点 /// 2.中序构建 bst /// </summary> /// <param name="left"></param> /// <param name="right"></param> /// <returns></returns> public TreeNode BuildTree(SingleListNode left, SingleListNode right) { //中序构建 if (left == right) { return(null); } var mid = FindMidNode(left, right); //构建 左节点 var leftnode = BuildTree(left, mid); //构建 根节点 var rootnode = new TreeNode(mid.val); //构建 右节点 var rightnode = BuildTree(mid.next, right); rootnode.left = leftnode; rootnode.right = rightnode; return(rootnode); }
public void GetIntersectionNodeTest_1() { int[] a = { 1, 2 }; int[] b = { 3, 4, 5 }; SingleListNode headA = LinkedListHelper.CreateSingleListedListByArray(a); SingleListNode headB = LinkedListHelper.CreateSingleListedListByArray(b); SingleListNode lastA = headA; SingleListNode lastB = headB; while (lastA.next != null) { lastA = lastA.next; } while (lastB.next != null) { lastB = lastB.next; } int[] same = { 6, 7, 8 }; for (int i = 0; i < same.Length; i++) { SingleListNode node = new SingleListNode(same[i]); lastA.next = node; lastB.next = node; lastA = lastA.next; lastB = lastB.next; } SingleListNode result = t141.GetIntersectionNode(headA, headB); Assert.IsTrue(result.val == 6); }
/// <summary>子类重载实现资源释放逻辑时必须首先调用基类方法</summary> /// <param name="disposing">从Dispose调用(释放所有资源)还是析构函数调用(释放非托管资源)</param> protected override void OnDispose(bool disposing) { base.OnDispose(disposing); Clear(); Top = null; }
static void Main(string[] args) { SingleListNode list1 = new SingleListNode(0, new SingleListNode(1, new SingleListNode(2, new SingleListNode(3, new SingleListNode(4, new SingleListNode(5)))))); SingleListNode list2 = new SingleListNode(100000, new SingleListNode(100001, new SingleListNode(100002))); Solution sd = new Solution(); var result = sd.MergeInBetween(list1, 3, 4, list2); }
static void Main(string[] args) { Solution solution = new Solution(); SingleListNode node = new SingleListNode(1); node.next = new SingleListNode(0); node.next.next = new SingleListNode(1); solution.GetDecimalValue(node); }
public void GetIntersectionNodeTest_4() //两条链表其实是同一条 { int[] ab = { 1, 2, 3, 4, 5, 8 }; SingleListNode headB = LinkedListHelper.CreateSingleListedListByArray(ab); SingleListNode headA = headB; SingleListNode result = t141.GetIntersectionNode(headA, headB); Assert.IsTrue(result.val == 1); }
public void GetIntersectionNodeTest_6() //节点的值都相同,但没有交点 { int[] a = { 1, 2, 3, 5, 6 }; SingleListNode headA = LinkedListHelper.CreateSingleListedListByArray(a); SingleListNode headB = LinkedListHelper.CreateSingleListedListByArray(a); SingleListNode result = t141.GetIntersectionNode(headA, headB); Assert.IsTrue(result == null); }
public void GetIntersectionNodeTest_5() //没有交点 { int[] a = { 1, 2 }; int[] b = { 3, 4, 5 }; SingleListNode headA = LinkedListHelper.CreateSingleListedListByArray(a); SingleListNode headB = LinkedListHelper.CreateSingleListedListByArray(b); SingleListNode result = t141.GetIntersectionNode(headA, headB); Assert.IsTrue(result == null); }
public override void PassNext(string value) { if (Next == null) { Next = new SingleListNode(value); } else { Next.PassNext(value); } }
/// <summary>使用一个集合初始化</summary> /// <param name="collection"></param> public ConcurrentStack(IEnumerable <T> collection) { SingleListNode <T> node = null; foreach (T local in collection) { var node2 = new SingleListNode <T>(local); node2.Next = node; node = node2; } Top = node; }
public SingleListNode FindMidNode(SingleListNode left, SingleListNode right) { SingleListNode fast = left; SingleListNode slow = left; while (fast != right && fast.next != right) { fast = fast.next; fast = fast.next; slow = slow.next; } return(slow); }
private void PushCore(SingleListNode <T> head, SingleListNode <T> tail) { var wait = new SpinWait(); do { wait.SpinOnce(); // 设置新对象的下一个节点为当前栈顶 tail.Next = Top; } // 比较并交换 // 如果当前栈顶第一个参数的Top等于第三个参数,表明没有被别的线程修改,保存第二参数到第一参数中 // 否则,不相等表明当前栈顶已经被修改过,操作失败,执行循环 while (Interlocked.CompareExchange(ref Top, head, tail.Next) != tail.Next); }
public void GetIntersectionNodeTest_2() //B链表只包含A链表的最后一个节点 { int[] a = { 1, 2, 3, 4, 5, 7 }; SingleListNode headA = LinkedListHelper.CreateSingleListedListByArray(a); SingleListNode headB = null; SingleListNode lastA = headA; while (lastA.next != null) { lastA = lastA.next; } headB = lastA; //B链表只包含A链表的最后一个节点 SingleListNode result = t141.GetIntersectionNode(headA, headB); Assert.IsTrue(result.val == 7); }
/// <summary> /// 双指针 /// </summary> /// <param name="head"></param> /// <param name="k"></param> /// <returns></returns> public SingleListNode GetKthFromEnd(SingleListNode head, int k) { SingleListNode first = head; SingleListNode second = head; while (k-- > 0) { first = first.next; } while (first != null) { first = first.next; second = second.next; } return(second); }
public SingleListNode MergeInBetween(SingleListNode list1, int a, int b, SingleListNode list2) { var currentnode1 = list1; int i = 0; while (true) { if (i == a - 1) { break; } i = i + 1; currentnode1 = currentnode1.next; } var currentnode2 = currentnode1; int j = 0; while (true) { if (j == (b - a + 2)) { break; } j = j + 1; currentnode2 = currentnode2.next; } currentnode1.next = list2; while (currentnode1.next != null) { currentnode1 = currentnode1.next; } currentnode1.next = currentnode2; return(list1); }
//T141 判断单向链表有没有环 public bool HasCycle(SingleListNode head) { if (head == null || head.next == null) { return(false); } SingleListNode slow = head, fast = head.next; //使用快慢指针,如果快指针追上了慢指针,说明产生了环 while (fast != slow) { if (fast == null || fast.next == null) { return(false); } fast = fast.next.next; slow = slow.next; } return(true); }
public int GetDecimalValue(SingleListNode head) { StringBuilder sb = new StringBuilder(head.val.ToString()); while (true) { if (head.next == null) { break; } sb.Append(head.next.val); head = head.next; } string strvalue = sb.ToString(); return(Convert.ToInt32(strvalue, 2)); }
/// <summary>清空</summary> public void Clear() { var top = Top; _Count = 0; Top = null; for (var node = top; node != null; node = node.Next) { if (UseNodePool) { PushNode(node); } else { // 断开关系链,避免内存泄漏 node.Next = null; node.Item = default(T); } } }
public void GetIntersectionNodeTest_3() //A链表只有一个节点,即是B链表的最后一个节点 { int[] b = { 1, 2, 3, 4, 5, 8 }; SingleListNode headB = LinkedListHelper.CreateSingleListedListByArray(b); SingleListNode headA = null; SingleListNode lastB = headB; while (lastB.next != null) { lastB = lastB.next; } int[] same = { 9 }; SingleListNode node = new SingleListNode(same[0]); lastB.next = node; headA = node; SingleListNode result = t141.GetIntersectionNode(headA, headB); Assert.IsTrue(result.val == 9); }
public TreeNode SortedListToBST(SingleListNode head) { return(BuildTree(head, null)); }
//T160 两个链表有可能是相交的。如果它们确实相交,返回相交的起始节点 // 假设:链表均没有环状 // 要求:两个链表要保持原有结构;如果链表没有相交,则返回null public SingleListNode GetIntersectionNode(SingleListNode headA, SingleListNode headB) { //使用字典实现--------------------------------------------------------------------- //if (headA == null || headB == null) return null; //Dictionary<int, SingleListNode> dictOfListA = new Dictionary<int, SingleListNode>(); //SingleListNode walk = headA; //for (int i = 0; walk != null; i++) //{ // dictOfListA.Add(i, walk); // walk = walk.next; //} //walk = headB; //while (walk != null) //{ // if (dictOfListA.ContainsValue(walk)) // { // return walk; // } // walk = walk.next; //} //return null; //不使用额外空间的实现-------------------------------------------------------------- //这个解法在遇到大量数据的时候会超出时间限制…………………… //if (headA == null || headB == null) return null; //SingleListNode pa = headA; //SingleListNode pb = headB; //while (pa != null) //{ // pb = headB; // SingleListNode paa = pa.next; // pa.next = null; //把链表A从这个位置断开,用于观察链表B是否被阻断 // while (pb.next != null) pb = pb.next; //链表B遍历 // pa.next = paa; //把链表A断开的地方连上 // if (pa == pb) return pa; //如果链表B遍历停止且指向链表A断开的位置,则断开的位置就是相交点 // else // { // pa = pa.next; // } //} //return null; //能够通过大量数据测试的实现-------------------------------------------------------------- //由于两个链表的长度可能不同,所以移动较长的链表指针向前走几个节点,到达和较短的链表同样的长度 //再逐个比较 if (headA == null || headB == null) { return(null); } int lengthA = 0, lengthB = 0; SingleListNode pa = headA, pb = headB; while (pa != null) { ++lengthA; pa = pa.next; } while (pb != null) { ++lengthB; pb = pb.next; } pa = headA; pb = headB; int diff = lengthA >= lengthB ? lengthA - lengthB : lengthB - lengthA; if (lengthA - lengthB > 0) { while (diff-- > 0) { pa = pa.next; } } else if (lengthB - lengthA > 0) { while (diff-- > 0) { pb = pb.next; } } while (pa != null && pb != null) { if (pa == pb) { return(pa); } pa = pa.next; pb = pb.next; } return(null); //评论区里的一种飘逸的实现-------------------------------------------------------------- //定义两个指针, 第一轮让两个到达末尾的节点指向另一个链表的头部, 最后如果相遇则为交点(在第一轮移动中恰好抹除了长度差) //两个指针等于移动了相同的距离, 有交点就返回, 无交点就是各走了两条指针的长度 //if (headA == null || headB == null) return null; //SingleListNode pA = headA, pB = headB; // 在这里第一轮体现在pA和pB第一次到达尾部会移向另一链表的表头, 而第二轮体现在如果pA或pB相交就返回交点, 不相交最后就是null==null //while (pA != pB) //{ // pA = pA == null ? headB : pA.next; // pB = pB == null ? headA : pB.next; //} //return pA; }
public SingleListNode(SingleListNode newNextNode) { this.next = newNextNode; }
public SingleListNode() { this.next = null; }
public void deleteNode(SingleListNode node) { node.val = node.next.val; node.next = node.next.next; }