public Node71 Insert(int v) { Node71 newNode = new Node71(v); newNode.next = this; return(newNode); }
/// <summary> /// Inserts a new node with the given value, after the specified node. /// If the specified node alreay has a next value, the new node takes on that value. /// Returns the new node /// </summary> /// <param name="node"></param> /// <param name="v"></param> /// <returns></returns> public Node71 Append(Node71 node, int v) { Node71 newNode = new Node71(v); newNode.next = node.next; node.next = newNode; return(newNode); }
// Merge two sorted lists public static Node71 MergeLists(Node71 list1, Node71 list2) { // special cases if (list2 == null) { return(list1); } if (list1 == null) { return(list2); } // "pointers" to iterate through the lists Node71 p1 = list1; Node71 p2 = list2; // resulting list Node71 pointer; Node71 mergedList; if (p1.value < p2.value) { mergedList = p1; p1 = p1.next; } else { mergedList = p2; p2 = p2.next; } pointer = mergedList; // go through both lists while (p1 != null && p2 != null) { if (p1.value < p2.value) { pointer.next = p1; pointer = p1; if (p1 != null) { p1 = p1.next; } } else { pointer.next = p2; pointer = p2; if (p2 != null) { p2 = p2.next; } } } // deal with unequal length lists if (p1 == null) { pointer.next = p2; } else { pointer.next = p1; } return(mergedList); }