public static LinkedNode <T> GetIntersectionNode <T>(LinkedNode <T> headA, LinkedNode <T> headB) { LinkedNode <T> A = headA, B = headB; while (A != B) { A = A != null ? A.Next : headB; B = B != null ? B.Next : headA; } return(A); }
public void RemoveTargetNode(LinkedNode <T> target) { if (target.Next != null) { target.Value = target.Next.Value; target.Next = target.Next.Next; } else { RemoveLast(); } }
private static int GetListLength <T>(LinkedNode <T> head) { int count = 0; while (head != null) { count++; head = head.Next; } return(count); }
public void AddLast(T newValue) { var newNode = new LinkedNode <T>(newValue); if (Head == null) { Head = newNode; } else { Last().Next = newNode; } }
public static LinkedNode <T> RecursiveReserve <T>(LinkedNode <T> node) { if (node == null || node.Next == null) { return(node); } var temp = RecursiveReserve <T>(node.Next); node.Next.Next = node; node.Next = null; return(temp); }
public void Reverse() { var cur = Head; LinkedNode <T> prev = null; while (cur != null) { var temp = cur.Next; cur.Next = prev; prev = cur; cur = temp; } Head = prev; }
public static LinkedNode <T> DeleteNode <T>(LinkedNode <T> head, int val) { if (head.Value.Equals(val)) { return(head.Next); } var prev = head; while (prev.Next.Value.Equals(val)) { prev = prev.Next; } prev.Next = prev.Next.Next; return(head); }
public static LinkedNode <T> Reverse <T>(LinkedNode <T> head) { var cur = head; LinkedNode <T> prev = null; while (cur != null) { var temp = cur.Next; cur.Next = prev; prev = cur; cur = temp; } return(prev); }
public static T MiddleValue <T>(LinkedNode <T> head) { var p1 = head; var p2 = head; while (p2.Next != null) { p1 = p1.Next; p2 = p2.Next.Next; if (p1 == p2) { throw new Exception("Cannot find middle value since linked list is circled"); } } return(p1.Value); }
public static bool IsCircled <T>(LinkedNode <T> head) { var p1 = head; var p2 = head; while (p2.Next != null) { p1 = p1.Next; p2 = p2.Next.Next; if (p1 == p2) { return(true); } } return(false); }
public static LinkedNode <T> Append <T>(LinkedNode <T> head, T newValue) { var newNode = new LinkedNode <T>(newValue); if (head == null) { return(newNode); } var node = head; while (node.Next != null) { node = node.Next; } node.Next = newNode; return(head); }
public LinkedNode Sum(LinkedNode head1, LinkedNode head2) { if (head1 == null && head2 == null) { throw new Exception("Both input linked lists are empty"); } if (head1 == null || head2 == null) { return(head1 ?? head2); } var preDigit = 0; LinkedNode res = null; LinkedNode head = null; while (head1 != null || head2 != null || preDigit != 0) { var list1Val = head1?.Value ?? 0; var list2Val = head2?.Value ?? 0; var sum = list1Val + list2Val + preDigit; var digitRes = sum % 10; preDigit = sum / 10; if (res == null) { res = new LinkedNode(digitRes); head = res; } else { res.Next = new LinkedNode(digitRes); res = res.Next; } head1 = head1?.Next; head2 = head2?.Next; } return(head); }
public static LinkedNode <T> DeleteNode2 <T>(LinkedNode <T> head, T val) { var cur = head; LinkedNode <T> prev = null; while (!cur.Value.Equals(val)) { prev = cur; cur = cur.Next; } if (prev == null) { head = head.Next; } else { prev.Next = cur.Next; } return(head); }
public static LinkedNode <T> CommonNodeOfTwoLinkedList <T>(LinkedNode <T> headA, LinkedNode <T> headB) { var lengthA = GetListLength(headA); var lengthB = GetListLength(headB); var diff = lengthA - lengthB; if (diff < 0) { var temp = headA; headA = headB; headB = temp; diff = -diff; } while (diff > 0) { headA = headA.Next; diff--; } while (headA != headB) { headA = headA.Next; headB = headB.Next; } return(headA); }