public void RemoveAt(int location) { if (Head == null) { Console.WriteLine("there isn't a list!"); } else { SllNode runner = Head; if (location < 1) { Head = runner.Next; } else { while (runner.Next.Next != null || location < 0) { if (location == 1) { runner.Next = runner.Next.Next; break; } runner = runner.Next; location -= 1; } } } }
private static BinaryTreeNode <T> GetBstFromSortedListInternalUtil(ref SllNode <T> currentSllNode, int n) { if (n <= 0) { return(null); } //Convert n/2 nodes to left side tree BinaryTreeNode <T> leftRoot = GetBstFromSortedListInternalUtil(ref currentSllNode, n / 2); //Make current node as root - ASSUME recursion would have incremented pointer in list BinaryTreeNode <T> root = new BinaryTreeNode <T>(currentSllNode.Data); root.Left = leftRoot; //Critical - move ahead in sortedlist currentSllNode = currentSllNode.Next; //Convert n/2 nodes to left side tree BinaryTreeNode <T> rightRoot = GetBstFromSortedListInternalUtil(ref currentSllNode, n - n / 2 - 1); root.Right = rightRoot; return(root); }
public void Remove() { if (Head == null) { return; } else { SllNode runner1 = Head; SllNode runner = Head.Next; if (runner == null) { Head = null; return; } else { while (runner.Next != null) { runner1 = runner; runner = runner.Next; } runner1.Next = null; return; } } }
public void RemoveAt(int n) { if (Head == null) { System.Console.WriteLine("List is already empty"); } else if (n == 0) { Head = Head.Next; } else { int count = 0; SllNode runner = Head; while (runner.Next.Next != null) { if (count == n - 1) { runner.Next = runner.Next.Next; } runner = runner.Next; count++; } if (count == n) { Remove(); } } }
public int Remove() { var runner = this.Head; ////This handles an empty list if (this.Head == null) { return(0); } ////This handles if our List only has one node if (runner.Next == null) { int returnValue = runner.Value; this.Head = null; return(runner.Value); } else { while (runner != null) { ////This jumps sets us forward one node so we can grab it's value ////and set the .Next from our previous node to null. if (runner.Next.Next == null) { int returnValue = runner.Next.Value; runner.Next = null; return(returnValue); } runner = runner.Next; } } return(0); }
public bool RemoveAt(int n) { if ((Head == null) || (n < 0)) { return(false); } else if (n == 0) { Head = Head.Next; return(true); } else { SllNode runner1 = Head; SllNode runner2 = Head.Next; while (n - 1 > 0) { if (runner2 == null) { return(false); } else { runner1 = runner2; runner2 = runner2.Next; } n--; } runner1.Next = runner2.Next; return(true); } }
public static BinarySearchTree <T> GetBstFromSortedList(SinglyLinkedList <T> sortedList) { int n = sortedList.GetCount(); SllNode <T> head = sortedList.Head; BinaryTreeNode <T> root = BinarySearchTree <T> .GetBstFromSortedListInternalUtil(ref head, n); return(new BinarySearchTree <T>(root)); }
public SllNode Find(int val) { SllNode runner = Head; while (runner.Value != val) { runner = runner.Next; } return(runner); }
public IEnumerator <T> GetEnumerator() { SllNode <T> current = linkedList.Head; while (current != null) { yield return(current.Data); current = current.Next; } }
public void PrintVals() { SllNode runner = Head; while (runner.Next != null) { System.Console.WriteLine(runner.Value); runner = runner.Next; } System.Console.WriteLine(runner.Value); }
public SllNode RemoveAtN(int idx) { SllNode runner = Head; for (int i = 0; i < idx - 1; i++) { runner = runner.Next; } SllNode retVal = runner.Next; runner.Next = runner.Next.Next; return(retVal); }
public int Remove() { SllNode runner = Head; while (runner.Next.Next != null) { runner = runner.Next; } int retVal = runner.Value; runner.Next = null; return(retVal); }
private string GetStringEquivalent(SllNode mergedListHead) { StringBuilder sb = new StringBuilder(); var current = mergedListHead; while (current.Next != null) { current = current.Next; sb.AppendFormat("{0},", current.Val); } return(sb.ToString().TrimEnd(',')); }
public void RearrangeTests(string inputLists, string expectedList) { // Arrange SllNode head = ParseToNodes(inputLists)[0]; // Act Problems.Rearrange(head); // Assert string mergedListString = GetStringEquivalent(head); Assert.AreEqual(expectedList, mergedListString); }
public void Remove() { if (Head == null) { return; } SllNode runner = Head; while (runner.Next.Next != null) { runner = runner.Next; } runner.Next = null; }
public void PrintValues() { if (Head == null) { System.Console.WriteLine("List is empty"); } else { SllNode runner = Head; while (runner != null) { System.Console.WriteLine(runner.Value); } } }
public void Remove() { if (Head == null) { Console.WriteLine("there isn't a list!"); } else { SllNode runner = Head; while (runner.Next != null) { runner = runner.Next; } runner.Next = null; } }
public void PrintValues() { if (Head == null) { Console.WriteLine("there isn't a list!"); } else { SllNode runner = Head; while (runner.Next != null) { Console.WriteLine(runner.Value); runner = runner.Next; } } }
public SllNode FindInt(int n) { if (Head == null) { return(null); } SllNode runner = Head; while (runner != null) { if (runner.Value == n) { return(runner); } } return(null); }
public void EnQueue(T data) { SllNode <T> newNode = new SllNode <T>(data); if (this.linkedList.Head == null) { this.linkedList.PushToHead(newNode); this.front = newNode; this.rear = newNode; } else { this.rear.Next = newNode; this.rear = newNode; } this.Count++; }
public void Add(int value) { SllNode newNode = new SllNode(value); if (Head == null) { Head = newNode; } else { SllNode runner = Head; while (runner.Next != null) { runner = runner.Next; } runner.Next = newNode; } }
public void PrintValues() { if (Head == null) { Console.WriteLine("This list is empty."); return; } else { SllNode runner = Head; while (runner != null) { Console.WriteLine("Value is:" + runner.Value); runner = runner.Next; } return; } }
public T DeQueue() { if (!this.IsEmpty()) { T data = this.front.Data; this.front = this.front.Next; this.Count--; if (this.Count == 0) { this.front = null; this.rear = null; } return(data); } return(default(T)); }
/* * Given k sorted singly list, write a function to merge all * into once sorted singly linked list */ public static SllNode GetMergedList(IList <SllNode> headNodes) { SllNode mergeNodeHead = new SllNode(); SllNode lastNodeInMergeList = null; while (headNodes.Any(h => h.Next != null)) { SllNode minHead = null; foreach (var head in headNodes) { if (head.Next == null) { continue; } if (minHead == null) { minHead = head; } else if (head.Next.Val <= minHead.Next.Val) { minHead = head; } } if (mergeNodeHead.Next == null) { mergeNodeHead.Next = minHead.Next; lastNodeInMergeList = minHead.Next; } else { lastNodeInMergeList.Next = minHead.Next; lastNodeInMergeList = minHead.Next; } minHead.Next = minHead.Next.Next; } return(mergeNodeHead); }
private static IList <SllNode> ParseToNodes(string inputString) { var pairs = inputString.Split('#'); IList <SllNode> inputs = new List <SllNode>(pairs.Length); foreach (var pair in pairs) { SllNode headNode = new SllNode(); inputs.Add(headNode); SllNode current = headNode; var items = pair.Split(','); foreach (var item in items) { SllNode itemNode = new SllNode(); itemNode.Val = int.Parse(item); current.Next = itemNode; current = itemNode; } } return(inputs); }
private static void Rearrange(SllNode head, SllNode first, SllNode second) { if (second != null) // Can happen when list has only 1 item { if (second.Next == null) { head.Next = second; } else { Rearrange(head, second, second.Next); } second.Next = first; } else { head.Next = first; } first.Next = null; }
public SllNode Find(int value) { if (Head == null) { Console.WriteLine("this list is empty"); return(null); } else { SllNode runner = Head; while (runner != null) { if (runner.Value == value) { Console.WriteLine("find this item"); return(runner); } runner = runner.Next; } Console.WriteLine("can not find this item"); return(null); } }
public bool Find(int value) { if (Head == null) { Console.WriteLine("there isn't a list!"); return(false); } else { if (Head.Value == value) { return(true); } SllNode runner = Head; while (runner.Next != null) { if (runner.Value == value) { return(true); } } return(false); } }
public SllNode(int value) { Value = value; Next = null; }
public SllQueue() { front = null; rear = null; }