Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program for Printing Reverse of a Linked List Without Actually Reversing
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 ?
Using Array Storage
In this method, we will store the elements of the given linked list in an array and then print the elements in reverse order. This approach is straightforward and allows easy access to elements by index.
Example
We first traverse the linked list to get its size, create an array of that size, store all elements, and then print them in reverse order:
<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 reverse 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>
The linked list is: 1 2 3 4 5 6 The linked list in reverse order is: 6 5 4 3 2 1
Complexity Analysis
Time Complexity: O(N), where N is the size of the linked list.
Space Complexity: O(N), as we are using an extra array to store the linked list elements.
Using Recursion
In this approach, we use recursion to print elements in reverse order. The recursive function calls itself for the next node first, then prints the current node's value after the recursive call returns.
Example
This method elegantly prints the linked list elements in reverse order without using extra array space:
<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 reverse manner using recursion
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>
The linked list is: 1 2 3 4 5 6 The linked list in reverse order is: 6 5 4 3 2 1
Complexity Analysis
Time Complexity: O(N), where N is the size of the linked list.
Space Complexity: O(N), due to the recursive call stack that stores function calls.
Comparison
| Method | Time Complexity | Space Complexity | Advantages | Disadvantages |
|---|---|---|---|---|
| Array Storage | O(N) | O(N) | Simple iterative approach | Two passes through list, extra array |
| Recursion | O(N) | O(N) | Elegant, single pass | Stack overflow risk for large lists |
Conclusion
Both approaches effectively print a linked list in reverse order without modifying the original structure. The recursive method is more elegant and requires only one traversal, while the array method is more straightforward and avoids potential stack overflow issues for very large lists.
