
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Reverse of a Linked List Without Reversing in JavaScript
Linked lists are linear data structures with their memory not being in a consecutive manner. We will write a complete code in JavaScript with different approaches and examples to understand the process better.
Introduction to Problem
In the given problem, we are given a linked list and we have to print all of its elements in reverse order, but we don't have to reverse the given linked list.
For example ?
Given linked list: 1 2 3 4 5 6 Result: 6 5 4 3 2 1
We will use two methods to print the given linked list in the opposite manner, let us see them ?
Storing in Array
In this method, we will store the elements of the given linked list in the manner by which they are present in the linked list and then we will print the elements of the array in the reverse order because getting the index of the array is easy.
Example
As we array is of a fixed size, we will assume for now that the maximum number of elements in the linked list is 1000, so we will create the array of this size only. Let us see the code ?
<html> <body> <script> // creating class for linked list class Node { constructor(data){ this.value = data this.next = null } } // function to print elements of the linked list function print(head){ var temp = head while(temp != null) { document.write(temp.value + " ") temp = temp.next } } // function to print the elements in an opposite manner function reverse_LL(head) { var temp = head; // getting the size of the linked list count = 0; while(temp != null){ temp = temp.next count++; } var arr = new Array(count) temp = head; var i = 0 while(temp != null){ arr[i] = temp.value temp = temp.next i++ } while(i--) { document.write(arr[i] + " ") } } // creating the linked list var head = new Node(1) head.next = new Node(2) head.next.next = new Node(3) head.next.next.next = new Node(4) head.next.next.next.next = new Node(5) head.next.next.next.next.next = new Node(6) // printing the linked list document.write("The linked list is: ") print(head) document.write("<br>The linked list in reverse order is: ") reverse_LL(head) </script> </body> </html>
Time and Space Complexity
The time complexity of the above code is O(N), where N is the size of the linked list.
The space complexity of the above code is O(N), as we are using an extra array to store the linked list elements.
Note: In the above code, we haven't used the array size as 1000 and instead first gone through the linked list to find the size of it and later created an array of that size.
Using Recursion
In the above approach we are finding the size of the linked list first and then use the array to store the elements which make the code look longer. To overcome this issue, we can use the concept of recursion, in which we will create a function and pass the linked list as the parameter.
In the recursion function, we will use the base case that the current parameter is null, otherwise first we will call the next same function with the next node as the parameter and after that call, we will print the value of the current node.
Example
This will print the elements of the linked list in the opposite manner, without reversing the given linked list, let us see the code ?
<html> <body> <script> // creating class for linked list class Node{ constructor(data){ this.value = data this.next = null } } // function to print elements of the linked list function print(head){ var temp = head while(temp != null){ document.write(temp.value + " ") temp = temp.next } } // function to print the elements in the oposite manner function reverse_LL(head){ if(head == null) { return } reverse_LL(head.next) document.write(head.value + " ") } // creating the linked list var head = new Node(1) head.next = new Node(2) head.next.next = new Node(3) head.next.next.next = new Node(4) head.next.next.next.next = new Node(5) head.next.next.next.next.next = new Node(6) // printing the linked list document.write("The linked list is: ") print(head) document.write("<br>The linked list in reverse order is: ") reverse_LL(head) </script> </body> </html>
Time and Space Complexity
The time complexity of the above code is O(N), where N is the size of the linked list.
The space complexity of the above code is O(N), this factor is due to the stack size that will contain the recursive call elements.
Conclusion
In this tutorial, we have implemented a JavaScript program to print a given linked list in the reverse order in which it is without reversing the given linked list. We have added all the elements of the linked list in the array and printed in the reverse order in the first approach. In the second approach we have created a recursive function that will print the elements in the opposite manner. Time and space complexity of both the methods is O(N).