Skip to main content

Posts

Showing posts from May, 2023

Print a Linked List in Reverse Order | Linked List | Java | MyCodingNetwork

  Print a Linked List in Reverse Order After mastering the four standard operations on a Linked List - Creation, Traversal, Insertion, and Deletion - we will now proceed to the next topic: ' Printing a Linked List in Reverse Order '. It serves as a continuation of our previous discussions. We will build upon the topics and ideas that we have previously explored to further our understanding about Linked List. We 'll be using recursive approach for the implementation. For this a separate recursive function would be needed. Concept of stack is also implemented for print statement. Let's discuss the algorithm for the same: Algorithm: Create a function printReverse() , which takes 'head ' of the Linked List as the parameter. Take a temporary node ' cur ' and assign it with the head of the list. Create a base case which checks if cur==null . If base case is TRUE, then function would return . If base case is FALSE, then the statements following that base case

Insertion and Deletion of a Node in Linked List | Java | MyCodingNetwork

  Insertion of a new node & Deletion of an existing node After an exhilarating commencement of the Linked List series, in this post, we'll be exploring the artistry behind insertion and deletion operations in linked list. So let's now move on to the second problem statement of the series, i.e., write two functions for insertion and deletion of a node respectively, from linked list . Algorithm for inserting a node at pos position: Create a function insert () which takes head of the node, pos and the new node as the parameter. Take a base case, if pos==1, means the new node is to be made the head of the linked list. If the above case is not true, traverse the linked list and reach to position, pos-1 . Assign the new node with the address of the node at pos. Finally, assign the node at pos-1 with address of new node. Algorithm for deleting a node at pos position: Create a function deletion () which takes pos and the head of the LinkedList as parameter. Base case, if pos