
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
Check if a Singly Linked List is Palindrome in JavaScript
A singly linked list is a linear data structure that is stored in a non-contiguous way in the memory and each block is connected by holding the address of the next block also known as a node. A palindrome can be explained as a set of characters, digits, etc, and it reads the same from both the front and backside. We will be given a singly linked list and have to find whether the values stored at the nodes are equal from both the front and backside.
Input
1 -> 2 -> 3 -> 3 -> 2 -> 1 -> null
Output
Yes, the given linked list is a palindrome.
Explanation
We can see the first and the last node have the same value, the second and second last, and so on, each node at the same distance from the front and backside have the same value.
Input
1 -> 2 -> 3 -> 4 -> 2 -> 1 -> null
Output
No, the given linked list is not a palindrome.
Explanation
Here, the first and second nodes are equal to the last and second last node respectively but after that nodes don't have the same value.
Using Stack
In this approach, we will first create a linked list by using the class and then will define some basic functions to add the data to the linked list and to print the data present in the linked list.
Example
// class to create the structure of the nodes class Node{ constructor(data){ this.value = data; this.next = null; } } // function to print the linked list function print(head){ var temp = head; var ans = ""; while(temp.next != null){ ans += temp.value; ans += " -> " temp = temp.next } ans += temp.value ans += " -> null" console.log(ans) } // function to add data in linked list function add(data, head, tail){ return tail.next = new Node(data); } // function to find the string is palindrome or not function check(head){ var temp = head; var stack = []; // defining the stack while(temp != null){ stack.push(temp.value); temp = temp.next; } temp = head; while(temp != null){ if(temp.value != stack.pop()){ return false; } temp = temp.next; } return true; } // defining linked list var head = new Node(1) var tail = head tail = add(2,head, tail) tail = add(3,head, tail) tail = add(3,head, tail) tail = add(2,head, tail) tail = add(1,head, tail) console.log("The given linked list is: ") print(head) // calling function to check if the current linked list is a palindrome or not if(check(head)){ console.log("Yes, the given linked list is a palindrome"); } else{ console.log("No, the given linked list is not a palindrome"); }
Time and Space Complexity
The time complexity of the above code is O(N) where N is the length of the linked list.
The space complexity of the above code is O(N), as we are using a stack data structure to store the elements in it.
Using Recursion
In this method, we will first find the length of the given linked list and then we will traverse to the mid of the linked list using the recursion. If the length of the given linked list odd, we will return next of the mid node else mid node and for each call we will get the respective node from the back from recursive call.
Example
// class to create the structure of the nodes class Node{ constructor(data){ this.value = data; this.next = null; } } // function to print the linked list function print(head){ var temp = head; var ans = ""; while(temp.next != null){ ans += temp.value; ans += " -> " temp = temp.next } ans += temp.value ans += " -> null" console.log(ans) } // function to add data in linked list function add(data, head, tail){ return tail.next = new Node(data); } // recursive function function recursion(head, number, odd){ if(number == 0){ if(odd){ return head.next; } else{ return head; } } var temp = recursion(head.next, number-1, odd); // if the current value is not equal then don't move to the next node // by this we will not reach null in the end // indicated the not palindrome if(temp.value != head.value){ return temp; } else{ return temp.next; } } // function to check if the given linked list is palindrome or not function check(head){ var temp = head; var len = 0; // finding the length of the given linked list while(temp != null){ len++; temp = temp.next; } // calling the recursion function if(recursion(head, Math.floor(len/2), len & 1) == null){ return true; } else{ return false; } } // defining linked list var head = new Node(1) var tail = head tail = add(2,head, tail) tail = add(3,head, tail) tail = add(4,head, tail) tail = add(3,head, tail) tail = add(2,head, tail) tail = add(1,head, tail) console.log("The given linked list is: ") print(head) // calling function to check if the current linked list is a palindrome or not if(check(head)){ console.log("Yes, the given linked list is a palindrome"); } else{ console.log("No, the given linked list is not a palindrome"); }
Conclusion
In this tutorial, we have implemented a JavaScript program to check if the given linked list nodes contain the values in the palindrome or not. We have implemented two codes using the stack and the recursion which works with the time complexity of the O(N) and space complexity is O(N) for stack and O(1) for the recursive method (only if the recursive call data is not considered).