Given a list of business requirements: use the stack to the list in order of elements reversed.
Input: List = 3 -> 2 ->. 1 Output: 1--> 2 -> 3 Input: 9 -> 7 -> 4 -> 2 Output: 2 -> 4 -> 7 -> 9 algorithmic process: complexity of the algorithm : O (n-) . 1, through the list, all the nodes will be pushed onto the stack. 2, traversing the stack, and pop-up element successively from the stack in reverse order with the store. java code:
// Java program to reverse linked list // using stack import java.util.*; class GfG { /* Link list node */ static class Node { int data; Node next; } static Node head = null; /* Given a reference (pointer to pointer) to the head of a list and an int, push a new node on the front of the list. */ static void push( int new_data) { Node new_node = new Node(); new_node.data = new_data; new_node.next = (head); (head) = new_node; } // Function to reverse linked list static Node reverseList(Node head) { // Stack to store elements of list Stack<Node > stk = new Stack<Node> (); // Push the elements of list to stack Node ptr = head; while (ptr.next != null) { stk.push(ptr); ptr = ptr.next; } // Pop from stack and replace // current nodes value' head = ptr; while (!stk.isEmpty()) { ptr.next = stk.peek(); ptr = ptr.next; stk.pop(); } ptr.next = null; return head; } // Function to print the Linked list static void printList(Node head) { while (head != null) { System.out.print(head.data + " "); head = head.next; } } // Driver Code public static void main(String[] args) { /* Start with the empty list */ //Node head = null; /* Use push() to construct below list 1->2->3->4->5 */ push( 5); push( 4); push( 3); push( 2); push( 1); head = reverseList(head); printList(head); } } // This code is contributed by Prerna Saini.
c # code
// C# program to reverse linked list // using stack using System; using System.Collections.Generic; class GfG { /* Link list node */ public class Node { public int data; public Node next; } static Node head = null; /* Given a reference (pointer to pointer) to the head of a list and an int, push a new node on the front of the list. */ static void push( int new_data) { Node new_node = new Node(); new_node.data = new_data; new_node.next = (head); (head) = new_node; } // Function to reverse linked list static Node reverseList(Node head) { // Stack to store elements of list Stack<Node > stk = new Stack<Node> (); // Push the elements of list to stack Node ptr = head; while (ptr.next != null) { stk.Push(ptr); ptr = ptr.next; } // Pop from stack and replace // current nodes value' head = ptr; while (stk.Count != 0) { ptr.next = stk.Peek(); ptr = ptr.next; stk.Pop(); } ptr.next = null; return head; } // Function to print the Linked list static void printList(Node head) { while (head != null) { Console.Write(head.data + " "); head = head.next; } } // Driver Code public static void Main(String[] args) { /* Start with the empty list */ //Node head = null; /* Use push() to construct below list 1->2->3->4->5 */ push( 5); push( 4); push( 3); push( 2); push( 1); head = reverseList(head); printList(head); } } // This code contributed by Rajput-Ji
Algorithm 2 (iteration):
Time complexity: O (1)
// Iterative C program to reverse a linked list #include <stdio.h> #include <stdlib.h> /* Linked list node */ struct Node { int data; struct Node* next; }; //function to reverse the linked list static void reverse(struct Node** head_ref) { struct Node* prev = NULL; struct Node* current = *head_ref; struct Node* next = NULL; while(current != NULL) { //Store next next = current->next; //Move pointers one position ahead. prev = current; current = next; } *head_ref = prev; } /* function to push a node */ void push(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next =(*head_ref); (*head_ref) = new_node; } /* function to print linked list */ void printList(struct Node* head) { struct Node* temp = head; while( temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } /* Driver program to test above function */ int main() { /* Start with the empty list */ struct Node* head = NULL; push(&head, 20); push(&head, 4); push(&head, 15); push(&head, 85); printf("Given linked list\n"); printList(head); reverse(&head); printf("Reversed Linked List\n"); printList(head); }
Source: https://www.geeksforgeeks.org/program-to-reverse-a-linked-list-using-stack/