static void Main(string[] args) { LinkListProblems llist = new LinkListProblems(); ListNode l1 = new ListNode(1); //l1.next = new ListNode(4); //l1.next.next = new ListNode(3); ListNode l2 = new ListNode(9); l2.next = new ListNode(9); //l2.next.next = new ListNode(4); llist.AddTwoNumbers(l1,l2); }
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) { ListNode resL = new ListNode(0); ListNode pL1 = l1; ListNode pL2 = l2; ListNode result = resL; int sum = int.MinValue; int preValue = 0; while (pL1 != null && pL2 != null) { sum = pL1.val + pL2.val + preValue; preValue = sum / 10; sum = sum % 10; resL.next = new ListNode(sum); pL1 = pL1.next; pL2 = pL2.next; resL = resL.next; } while (pL1 != null) { sum = pL1.val + preValue; preValue = sum / 10; sum = sum % 10; resL.next = new ListNode(sum); pL1 = pL1.next; resL = resL.next; } while (pL2 != null) { sum = pL2.val + preValue; preValue = sum / 10; sum = sum % 10; resL.next = new ListNode(sum); pL2 = pL2.next; resL = resL.next; } if (preValue > 0) { resL.next = new ListNode(preValue); } return result.next; }
private ListNode <T> this[long index] { get { if (index < 0 || index > this.Count - 1) { throw new ArgumentOutOfRangeException("index", "Index cannot be negative or bigger than the count of elements - 1."); } long i = 0; ListNode <T> currentNode = this.head; while (i != index) { currentNode = currentNode.NextNode; i++; } return(currentNode); } }
public void Test(int[] input, int[] output, int target) { var head = CreateLinkedList(input); var empty = new ListNode(-1, head); var copyEmpty = empty; while (empty.next != null) { if (empty.next.val == target) { empty.next = empty.next?.next; } else { empty = empty.next; } } ToArray(copyEmpty.next).Should().BeEquivalentTo(output); }
private T RemoveAt(int index) { if (index >= count || index < 0) { throw new ArgumentOutOfRangeException("Invalid index: " + index); } // Find the element at the specified index int currentIndex = 0; ListNode currentNode = this.head; ListNode prevNode = null; while (currentIndex < index) { prevNode = currentNode; currentNode = currentNode.NextNode; currentIndex++; } // Remove the foung element from the list of nodes RemoveListNode(currentNode, prevNode); return currentNode.Element; }
public static LinkedList.ListNode MoveZerosToFront(LinkedList.ListNode head) { var currentNode = head; while (currentNode != null) { if (currentNode.Next != null) { if (currentNode.Next.Data == 0) { var nodeToBeMoved = currentNode.Next; currentNode.Next = nodeToBeMoved.Next; nodeToBeMoved.Next = head; head = nodeToBeMoved; } } currentNode = currentNode.Next; } return(head); }
public ListNode MergeTwoLists(ListNode l1, ListNode l2) { ListNode list1 = l1; ListNode list2 = l2; if (list1 == null && list2 == null) { return(list1); } List <ListNode> arrList = new List <ListNode>(); while (list1 != null) { arrList.Add(list1); list1 = list1.next; } while (list2 != null) { arrList.Add(list2); list2 = list2.next; } List <ListNode> SortedList = arrList.OrderBy(o => o.val).ToList(); ListNode head = new ListNode(SortedList[0].val); ListNode temp = head; for (int i = 1; i < SortedList.Capacity; i++) { ListNode newNode = new ListNode(SortedList[i].val); temp.next = newNode; temp = newNode; } return(head); }
private void RemoveListNode(ListNode node, ListNode prevNode) { count--; if (count == 0) { // The list becomes empty this.head = null; this.tail = null; } else if (prevNode == null) { // The head node was removed --> update the head this.head = node.NextNode; } else { prevNode.NextNode = node.NextNode; } // Fix the tail in case it was removed if (object.ReferenceEquals(this.tail, node)) { this.tail = prevNode; } }
public ListNode CreateLinkedList(int[] numbers) { if (numbers == null || !numbers.Any()) { return(null); } if (numbers.Length == 1) { return(new ListNode(numbers.First())); } var root = new ListNode(numbers.First()); var current = root; foreach (var number in numbers.Skip(1)) { var newNode = new ListNode(number); current.next = newNode; current = newNode; } return(root); }
public DynamicList() { this.head = null; this.tail = null; this.count = 0; }
static void Main(string[] args) { Program p = new Program(); //FrequencyLinkedListNode root= p.GetWordCountsInLinkedList("this is a this is mega a a a this this this ".Split().ToList()); //Console.WriteLine(root); //FrequencyLinkedListNode cur = root; //while (cur != null) //{ // Console.WriteLine(cur.word+" "+cur.frequency); // cur=cur.next; //} //VertexedLinkedListNode root, n1, n2,n3,n4,n5,n6; //n6 = new VertexedLinkedListNode(8, null, true); //n5 = new VertexedLinkedListNode(8, n6, true); //n4 = new VertexedLinkedListNode(8, n5, true); //n3 = new VertexedLinkedListNode(8, n4, true); //n2 = new VertexedLinkedListNode(8, n3, true); //n1 = new VertexedLinkedListNode(8, n2, true); //root = new VertexedLinkedListNode(5, n1, true); //Console.WriteLine(p.MaxDistanceBetweenVertices(root)); //ListNode n2 = null; //ListNode n4 = new ListNode(6, n2); //ListNode n3 = new ListNode(5, n4); //n2 = new ListNode(4, n3); //n4.next = n2; //ListNode root = new ListNode(3, n2); //LinkedList ll = new LinkedList(root); //// root1 = p.Merge(root1, root2); //while (root1 != null) //{ // Console.WriteLine(root1.data); // root1 = root1.next; //} // Console.WriteLine(ll.ToString()); //Console.Write(ll.DetectCycle(ll.head)); //Console.WriteLine(ll.GetCycleListLength(ll.head)); //DoubleListNode n1, n2, n3, n4, n5; //n1 = new DoubleListNode(1, 0); //n2 = new DoubleListNode(2, 1); //n3 = new DoubleListNode(3, 2); //n4 = new DoubleListNode(4, 1); //PriorityQueue pq = new PriorityQueue(); //pq.Enqueue(n1); //pq.Enqueue(n2); //pq.Enqueue(n3); //pq.Enqueue(n4); //DoubleListNode d = pq.Deque(); //pq.Enqueue(new DoubleListNode(5, 0)); //d = pq.Deque(); //d = pq.Deque(); //Console.WriteLine(d.data + " " + d.priority); ListNode root, n1,n2,n3,n4,n5,n6; n6 = new ListNode(7, null); n5 = new ListNode(6, n6); n4 = new ListNode(5, n5); n3 = new ListNode(4, n4); n2 = new ListNode(3, n3); n1 = new ListNode(2, n2); root = new ListNode(1, n1); //ListNode root1, n3, n4; //n4 = new ListNode(10, null); //n3 = new ListNode(6, n4); //root1 = new ListNode(3, n3); ListNode cur = p.SwapAdjacent2(root); while (cur != null) { Console.WriteLine(cur); cur = cur.next; } }
public void Remove(ListNode <T> target) { var index = target._index; Remove(index); }
internal bool DetectCycle(ListNode head) { if (head.next == null || head == null) return false; ListNode slow = head; ListNode fast = head.next; while (slow != null && fast != null && fast.next !=null) { if (fast.next == slow || fast == slow) return true; else { slow = slow.next; fast = fast.next.next; } } return false; }
public ListNode <T> AddBefore(ListNode <T> target, T value) { return(_reverse.Add(target, value)); }
public ListNode <T> AddAfter(ListNode <T> target, T value) { return(_forward.Add(target, value)); }
public LinkedList(ListNode head) { this.head = head; }
internal int GetCycleListLength(ListNode head) { if (head == null) return 0; int count = 0; ListNode slow = head; ListNode fast = head; while (fast.next != null) { slow = slow.next; fast = fast.next.next; count++; if (fast == slow) break; } slow = head; while (fast != slow) { count++; slow = slow.next; fast = fast.next; } return count; }
ListNode SwapAdjacent2(ListNode head) { ListNode cur = head; ListNode temp= null; while (cur != null && cur.next != null) { ListNode forward = cur.next; cur.next = forward.next; forward.next = cur; if (cur == head) head = forward; if (temp != null) temp.next = forward; temp = cur; cur = cur.next; } return head; }
public ListNode(T element, ListNode prevNode) { this.Element = element; prevNode.NextNode = this; }
internal ListNode GetCycleStartNode(ListNode head) { if (head == null ) return null; ListNode slow = head; ListNode fast = head; while (fast.next !=null) { slow = slow.next; fast = fast.next.next; if (fast == slow) break; } slow = head; while (fast != slow) { slow = slow.next; fast = fast.next; } return fast; }
public ListNode(int data, ListNode next) { this.data = data; this.next = next; }
ListNode SwapAdjacent(ListNode head) { ListNode prev = null, cur = head; while (cur != null && cur.next != null) { if (prev == null) { head = cur.next; ListNode temp = cur.next.next; cur.next.next = cur; cur.next = temp; prev = cur; } else { prev.next = cur.next; cur.next = cur.next.next; prev.next.next = cur; prev = cur; } cur = cur.next; } return head; }
//Merge in merge sort for linked list ListNode Merge(ListNode h1, ListNode h2) { if (h2 == null) return h1; if (h1 == null) return h2; ListNode cur1 = h1, cur2 = h2; while (cur2 != null) { if (cur1.data >= cur2.data) { ListNode temp = cur2.next; cur2.next = cur1; cur1 = cur2; cur2 = temp; } else { while ( cur1.next != null && cur1.next.data < cur2.data) { cur1 = cur1.next; } if (cur1.next == null) { cur1.next = cur2; return cur1; } else { ListNode temp = cur2.next; cur2.next = cur1.next; cur1.next = cur2; cur2 = temp; } } } return h1; }
public ListNode(T element) { this.Element = element; NextNode = null; }