public static SumNode SumOfLinkedListReverse(Node l1, Node l2, int carry) { if (l1 == null && l2 == null && carry == 0) { return(null); } SumNode resultNode = new SumNode(); int value = 0; if (l1 != null) { value += l1.data; } if (l2 != null) { value += l2.data; } value += carry; resultNode.result = new Node(value); resultNode.sum = value % 10; SumOfLinkedListReverse(l1 == null ? null : l1.next, l2 == null ? null : l2.next, value >= 10 ? 1 : 0); SumNode.resultList = LinkedList.Append(SumNode.resultList, resultNode.sum); resultNode.result = SumNode.resultList; return(resultNode); }
static void Main(string[] args) { Node head = null; LinkedList.Append(ref head, 001); LinkedList.Append(ref head, 002); LinkedList.Append(ref head, 003); LinkedList.Append(ref head, 004); Console.WriteLine("Linked list:"); LinkedList.Print(head); LinkedList.Reverse(ref head); Console.WriteLine(); Console.WriteLine("Reversed Linked list:"); LinkedList.Print(head); Console.WriteLine(); //LinkedList.PrintRecursive(head); Console.WriteLine("Reverse of Reversed Linked list:"); LinkedList.ReverseUsingRecursion(head); head = LinkedList.newHead; LinkedList.PrintRecursive(head); Console.ReadKey(); }
public static void CreateLinkedList() { LinkedList.Append(LinkedList.Head, 10); LinkedList.Append(LinkedList.Head, 20); LinkedList.Append(LinkedList.Head, 30); LinkedList.Append(LinkedList.Head, 30); LinkedList.Append(LinkedList.Head, 40); }
/// <summary> /// First create an empty LinkedList named linkedlist /// Then use Append method to add new nodes to linkedlist /// Finally, use InsertBefore method to insert a new node with a value of 10 before the node with a value of 5 /// </summary> /// <param name="args"></param> static void Main(string[] args) { LinkedList linkedlist = new LinkedList(); linkedlist.Append(1); linkedlist.Append(2); linkedlist.Append(3); linkedlist.Append(4); linkedlist.Append(5); linkedlist.Append(6); linkedlist.InsertBefore(5, 10); }