//Bottom-Up Recursive Solution. //Line 1+2+5 => for return the tail of the list as new head. //Line 3+4 => for reverse the pointer while bottom-up. public ListNode ReverseLinkedList_Recursive_BottomUp(ListNode head) { if (head == null || head.next == null) return head; //Line 1: "head == null" is used to handle empty linkedlist. "head.next == null" is used to return the newHead(tail). ListNode tail = ReverseLinkedList_Recursive_BottomUp(head.next); //Line 2: Recurse till the last node, and return it. head.next.next = head; //Line 3: Change From [ prev -> head -> next ] to [ prev -> head <=> next ] head.next = null; //Line 4: Change From [ prev -> head <=> next ] to [ prev -> head <- next ] + [ head -> null ] return tail; //Line 5: work with Line 1 and 2 to pass the last node from deepest recursion to the top. Ensure the return value is the new head. }
//Delete the given node from the LinkedList (node cannot be tail node.) public void DeleteGivenNode(ListNode node) { if (node.next != null) { node.val = node.next.val; node.next = node.next.next; } }
//Delete all nodes that has the given value. Elegant Recursive solution. public ListNode DeleteGivenValue_Recursive(ListNode head, int value) { //Tail if (head == null) return null; //Delete node if (head.val == value) return DeleteGivenValue_Recursive(head.next, value); //Recurse all node. else head.next = DeleteGivenValue_Recursive(head.next, value); return head; }
public ListNode CreateLinkedList(int[] element) { ListNode prev = new ListNode(0); ListNode head = prev; for (int i = 0; i < element.Length; i++) { ListNode newNode = new ListNode(element[i]); prev.next = newNode; prev = prev.next; } return head.next; }
//Delete all nodes that has the given value. Iterative solution using dummy head. public ListNode DeleteGivenValue_Iterative(ListNode head, int value) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode curr = dummy; while (curr.next != null) { if (curr.next.val == value) curr.next = curr.next.next; else curr = curr.next; } return dummy.next; }
//In each loop, Focus on reversing the "current node" (i.e. head) only. //Do not try to do too many things in one loop, because it confuse people. public ListNode ReverseLinkedList_Iterative(ListNode head) { //For the first node, prevNode is null //For the last node (null, loop exit condition), prevNode is the first node of the reversed linkedlist. ListNode prevNode = null; while (head != null) { ListNode nextNode = head.next; //Before pointing "head.next" to the "prevNode", save the node "head.next" originally pointing at to "tail". head.next = prevNode; //Reverse the current node by pointing "head.next" to the "prevNode". prevNode = head; //Current node completed. Move on to next node. Since the focus changed from head to next, prevNode should be changed to head. head = nextNode; //Since the focus changed from head to next, head should be changed to tail. } return prevNode; }
/// <summary> /// find the size of the stack using linked list. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="head">The head.</param> /// <returns></returns> public static int SizeSLL <T>(ListNode <T> head) { int count = 0; if (head == null) { return(count); } else { ListNode <T> temp = head; while (temp != null) { count++; temp = temp.next; } } return(count); }
public static ListNode <T> Add <T>(ListNode <T> head, ListNode <T> newnode) { ListNode <T> temp; if (head == null) { head = newnode; } else { temp = head; while (temp.next != null) { temp = temp.next; } temp.next = newnode; } return(head); }
public static ListNode <T> ArrayToOrderedLinkedList <T>(ListNode <T> head, T[] array) { ListNode <T> newnode = null; string data; for (int i = 0; i < array.Length; i++) { data = (string)((object)array[i]); if (data != null) { newnode = new ListNode <T>(); newnode.data = (T)((object)data); head = AddInOrder(head, newnode); } else { break; } } return(head); }
public static void LinkedListToFile <T>(ListNode <T> head, string path) { StreamWriter SW = new StreamWriter(path); if (head == null || !File.Exists(path)) { Console.WriteLine("head is null or file doesnot exist"); } else { ListNode <T> temp = head; while (temp != null) { SW.Write(((string)((object)temp.data)), true); SW.Write(" ", true); temp = temp.next; } } SW.Close(); }
/// <summary> /// calculate the size of linked list /// </summary> /// <param name="head"></param> /// <returns></returns> public static int ListSize <T>(ListNode <T> head) { int size = -1; if (head == null) { size = 1; return(size); } else { ListNode <T> temp = head; while (temp != null) { size++; temp = temp.next; } } return(size); }
/// <summary> /// Write the HashTable to a file /// </summary> /// <typeparam name="T"></typeparam> /// <param name="HashTable">The hash table.</param> /// <param name="path">The path.</param> public static void HashTableToFile <T>(ListNode <T>[] HashTable, string path) { StreamWriter SW = new StreamWriter(path); for (int i = 0; i < HashTable.Length; i++) { if (HashTable[i] != null || !File.Exists(path)) { ListNode <T> temp = HashTable[i]; while (temp != null) { string data = (string)((object)temp.data); Console.WriteLine("data to be written : {0}", data); SW.Write(data, true); SW.Write(" ", true); temp = temp.next; } SW.WriteLine(" ", true); } } SW.Close(); }
/// <summary> /// if the item is found then return the index otherwise return -1; /// </summary> /// <param name="head"></param> /// <param name="item"></param> /// <returns></returns> public static int Index <T>(ListNode <T> head, ListNode <T> item) { int index = -1; if (head == null) { return(index); } else { index = 0; ListNode <T> temp = head; while (temp != null) { if ((temp.data).Equals(item.data)) { return(index); } index++; temp = temp.next; } } return(index); }
public ListNode ReverseLinkedList_Recursive_TopDown(ListNode head) { return ReverseLinkedList(null, head); }
/// <summary> /// Determines whether the stack using the linked list is empty. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="head">The head.</param> /// <returns> /// <c>true</c> if [is empty stack ll] [the specified head]; otherwise, <c>false</c>. /// </returns> public static bool IsEmptyStackLL <T>(ListNode <T> head) { return(head == null); }
//Top-Down Recursive Solution. private ListNode ReverseLinkedList(ListNode prevNode, ListNode head) { if (head == null) return head; ListNode nextNode = head.next; //IDENTICAL TO Iterative solution. head.next = prevNode; //IDENTICAL TO Iterative solution. return ReverseLinkedList(head, nextNode); }
/// <summary> /// Creates the hash table using linked list. /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static ListNode <T>[] CreateHashTableUsingLinkedList <T>() { ListNode <T>[] HashTable = new ListNode <T> [11]; return(HashTable); }
/// <summary> /// Creates the stack using linked list. /// </summary> /// <returns></returns> public static ListNode <T> CreateStackUsingLinkedList <T>() { ListNode <T> head = null; return(head); }
/// <summary> /// Pop the item form the index out of the stack using linked list. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="head">The head.</param> /// <param name="index">The index.</param> /// <returns></returns> public static ListNode <T> Pop <T>(ref ListNode <T> head, int index) { ListNode <T> temp; ListNode <T> item; ref ListNode <T> refhead = ref head;