Given a singly linked list. The task is to check if the given linked list is palindrome or not.
Examples:
Input:
Output: true
Explanation: The given linked list is 1->2->1->1->2->1 , which is a palindrome and Hence, the output is true.
Input:
Output: false
Explanation: The given linked list is 1->2->3->4, which is not a palindrome and Hence, the output is false.
[Naive Approach - 1] Using Stack - O(n) Time and O(n) Space
The idea is to initialize an empty stack and start traversing from the head node. Push all the nodes into the stack while traversing till end. Again traverse the linked list and compare the popped node's data with the current node data. If both the data are not equal return false, else when stack becomes empty, it means we reach to the end node. return true, as linked list is a palindrome.
Below is the implementation of the above approach :
C++
// C++ program to check if
// linked list is palindrome.
#include <iostream>
#include <stack>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int d) {
data = d;
next = nullptr;
}
};
// Function to check if the linked list
// is palindrome or not
bool isPalindrome(Node* head) {
Node* currNode = head;
// Declare a stack
stack<int> s;
// Push all elements of the list to the stack
while (currNode != nullptr) {
s.push(currNode->data);
currNode = currNode->next;
}
// Iterate in the list again and check by
// popping from the stack
while (head != nullptr) {
// Get the topmost element
int c = s.top();
s.pop();
// Check if data is not same as popped element
if (head->data != c) {
return false;
}
// Move ahead
head = head->next;
}
return true;
}
int main() {
// Linked list : 1->2->3->2->1
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(2);
head->next->next->next->next = new Node(1);
bool result = isPalindrome(head);
if (result)
cout << "true\n";
else
cout << "false\n";
return 0;
}
Java
// Java program to check if
// linked list is palindrome.
import java.util.Stack;
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
class GfG {
// Function to check if the linked list
// is palindrome or not
static boolean isPalindrome(Node head) {
Node currNode = head;
Stack<Integer> s = new Stack<>();
// Push all elements of the list to the stack
while (currNode != null) {
s.push(currNode.data);
currNode = currNode.next;
}
// Iterate in the list again and check
// by popping from the stack
while (head != null) {
// Get the topmost element
int c = s.pop();
// Check if data is not same as
// popped element
if (head.data != c) {
return false;
}
// Move ahead
head = head.next;
}
return true;
}
public static void main(String[] args) {
// Linked list : 1->2->3->2->1
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(2);
head.next.next.next.next = new Node(1);
boolean result = isPalindrome(head);
if (result)
System.out.println("true");
else
System.out.println("false");
}
}
Python
# Python3 program to check if linked
# list is palindrome using stack
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Function to check if the linked list
# is palindrome or not
def isPalindrome(head):
curr_node = head
s = []
# Push all elements of the list to the stack
while curr_node is not None:
s.append(curr_node.data)
curr_node = curr_node.next
# Iterate in the list again and check by'
# popping from the stack
while head is not None:
# Get the topmost element
c = s.pop()
# Check if data is not same as popped element
if head.data != c:
return False
# Move ahead
head = head.next
return True
# Linked list : 1->2->3->2->1
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(2)
head.next.next.next.next = Node(1)
result = isPalindrome(head)
if result:
print("true")
else:
print("false")
C#
// C# program to check if
// linked list is palindrome.
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node next;
public Node(int d) {
data = d;
next = null;
}
}
// Class to check if the linked list
// is palindrome or not
class GfG {
// Function to check if the linked list
// is palindrome or not
static bool isPalindrome(Node head) {
Node currNode = head;
Stack<int> s = new Stack<int>();
// Push all elements of the list to the stack
while (currNode != null) {
s.Push(currNode.data);
currNode = currNode.next;
}
// Iterate in the list again and check by
// popping from the stack
while (head != null) {
// Get the topmost element
int c = s.Pop();
// Check if data is not same as
// popped element
if (head.data != c) {
return false;
}
// Move ahead
head = head.next;
}
return true;
}
static void Main(string[] args) {
// Linked list : 1->2->3->2->1
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(2);
head.next.next.next.next = new Node(1);
bool result = isPalindrome(head);
if (result)
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
JavaScript
// JavaScript program to check if
// linked list is palindrome.
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Function to check if the linked list is
// palindrome or not
function isPalindrome(head) {
let currNode = head;
let stack = [];
// Push all elements of the list to the stack
while (currNode !== null) {
stack.push(currNode.data);
currNode = currNode.next;
}
// Iterate in the list again and check by
// popping from the stack
while (head !== null) {
// Get the topmost element
let c = stack.pop();
// Check if data is not same as popped element
if (head.data !== c) {
return false;
}
// Move ahead
head = head.next;
}
return true;
}
// Linked list : 1->2->3->2->1
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(2);
head.next.next.next.next = new Node(1);
let result = isPalindrome(head);
if (result)
console.log("true");
else
console.log("false");
[Naive Approach - 2] Using Recursion - O(n) Time and O(n) Space
The idea is to initialize a pointer start, which will initially point to the head of the node. Then, recursively traverse the list. At each node end, first recursively check if the right side of the list is palindrome. If yes, then compare the values of the start and end node. If they are equal, then set start = start.next and return true. Otherwise return false.
C++
// C++ program to check if
// linked list is palindrome.
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int d) {
data = d;
next = nullptr;
}
};
// Recursive Function to check whether
// the list is palindrome
bool isPalindromeRecur(Node* end, Node* &start) {
// base case
if (end == nullptr) return true;
// Recursively check the right side.
bool right = isPalindromeRecur(end->next, start);
// Compare the start and end nodes.
bool ans = right && start->data == end->data;
// Update the start node
start = start->next;
return ans;
}
// Function to check whether the list is palindrome
bool isPalindrome(Node* head) {
// Set starting node to head
Node* start = head;
// Recursively check the ll and return
return isPalindromeRecur(head, start);
}
int main() {
// Linked list : 1->2->3->2->1
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(2);
head->next->next->next->next = new Node(1);
bool result = isPalindrome(head);
if (result)
cout << "true" << endl;
else
cout << "false" << endl;
return 0;
}
Java
// Java program to check if
// linked list is palindrome.
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
class GfG {
// Recursive Function to check whether
// the list is palindrome
static boolean isPalindromeRecur(Node end, Node[] start) {
// base case
if (end == null) return true;
// Recursively check the right side.
boolean right = isPalindromeRecur(end.next, start);
// Compare the start and end nodes.
boolean ans = right && start[0].data == end.data;
// Update the start node
start[0] = start[0].next;
return ans;
}
// Function to check whether the list is palindrome
static boolean isPalindrome(Node head) {
// Set starting node to head
Node[] start = new Node[]{head};
// Recursively check the ll and return
return isPalindromeRecur(head, start);
}
public static void main(String[] args) {
// Linked list : 1->2->3->2->1
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(2);
head.next.next.next.next = new Node(1);
boolean result = isPalindrome(head);
if (result)
System.out.println("true");
else
System.out.println("false");
}
}
Python
# Python program to check if
# linked list is palindrome.
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Recursive Function to check whether
# the list is palindrome
def isPalindromeRecur(end, start):
# base case
if end is None:
return True
# Recursively check the right side.
right = isPalindromeRecur(end.next, start)
# Compare the start and end nodes.
ans = right and start[0].data == end.data
# Update the start node
start[0] = start[0].next
return ans
# Function to check whether the list is palindrome
def isPalindrome(head):
# Set starting node to head
start = [head]
# Recursively check the ll and return
return isPalindromeRecur(head, start)
if __name__ == "__main__":
# Linked list : 1->2->3->2->1
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(2)
head.next.next.next.next = Node(1)
result = isPalindrome(head)
if result:
print("true")
else:
print("false")
C#
// C# program to check if
// linked list is palindrome.
class Node {
public int data;
public Node next;
public Node(int d) {
data = d;
next = null;
}
}
class GfG {
// Recursive Function to check whether
// the list is palindrome
static bool isPalindromeRecur(Node end, ref Node start) {
// base case
if (end == null) return true;
// Recursively check the right side.
bool right = isPalindromeRecur(end.next, ref start);
// Compare the start and end nodes.
bool ans = right && start.data == end.data;
// Update the start node
start = start.next;
return ans;
}
// Function to check whether the list is palindrome
static bool isPalindrome(Node head) {
// Set starting node to head
Node start = head;
// Recursively check the ll and return
return isPalindromeRecur(head, ref start);
}
static void Main(string[] args) {
// Linked list : 1->2->3->2->1
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(2);
head.next.next.next.next = new Node(1);
bool result = isPalindrome(head);
if (result)
System.Console.WriteLine("true");
else
System.Console.WriteLine("false");
}
}
JavaScript
// JavaScript program to check if
// linked list is palindrome.
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Recursive Function to check whether
// the list is palindrome
function isPalindromeRecur(end, start) {
// base case
if (end === null) return true;
// Recursively check the right side.
let right = isPalindromeRecur(end.next, start);
// Compare the start and end nodes.
let ans = right && start[0].data === end.data;
// Update the start node
start[0] = start[0].next;
return ans;
}
// Function to check whether the list is palindrome
function isPalindrome(head) {
// Set starting node to head
let start = [head];
// Recursively check the ll and return
return isPalindromeRecur(head, start);
}
// Linked list : 1->2->3->2->1
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(2);
head.next.next.next.next = new Node(1);
let result = isPalindrome(head);
if (result)
console.log("true");
else
console.log("false");
[Expected Approach] Using Iterative Method - O(n) Time and O(1) Space
The approach involves reversing the second half of the linked list starting from the middle. After reversing, traverse from the head of the list and the head of the reversed second half simultaneously, comparing the node values. If all corresponding nodes have equal values, the list is a palindrome.
Follow the steps below to solve the problem:
- Use two pointers say, fast and slow to find the middle of the list. fast will move two steps ahead and slow will move one step ahead at a time.
- if list is odd, when fast->next is NULL , slow will point to the middle node.
- if list is even, when fast->next->next is NULL slow will point to the middle node.
- Reverse the second half of the list starting from the middle.
- Check if the first half and the reversed second half are identical by comparing the node values.
- Restore the original list by reversing the second half again and attaching it back to the first half.
Below is the implementation of the above approach:
C++
// C++ program to check if a linked list is palindrome
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int d) {
data = d;
next = nullptr;
}
};
// Function to reverse a linked list
Node* reverse(Node* head) {
Node* prev = nullptr;
Node* curr = head;
Node* next;
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
// Function to check if two lists are identical
bool isIdentical(Node* n1, Node* n2) {
for (; n1 && n2; n1 = n1->next, n2 = n2->next)
if (n1->data != n2->data)
return 0;
// returning 1 if data at all nodes are equal.
return 1;
}
// Function to check whether the list is palindrome
bool isPalindrome(Node* head) {
if (!head || !head->next)
return true;
// Initialize slow and fast pointers
Node* slow = head;
Node* fast = head;
// Move slow to the middle of the list
while (fast->next && fast->next->next) {
slow = slow->next;
fast = fast->next->next;
}
// Split the list and reverse the second half
Node* head2 = reverse(slow->next);
slow->next = nullptr; // End the first half
// Check if the two halves are identical
bool ret = isIdentical(head, head2);
// Restore the original list
head2 = reverse(head2);
slow->next = head2;
return ret;
}
int main() {
// Linked list : 1->2->3->2->1
Node head(1);
head.next = new Node(2);
head.next->next = new Node(3);
head.next->next->next = new Node(2);
head.next->next->next->next = new Node(1);
bool result = isPalindrome(&head);
if (result)
cout << "true\n";
else
cout << "false\n";
return 0;
}
C
// C Program to check if a linked list is palindrome
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Function to reverse a linked list
struct Node* reverse(struct Node* head) {
struct Node* prev = NULL;
struct Node* curr = head;
struct Node* next;
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
// Function to check if two lists are identical
int isIdentical(struct Node* n1, struct Node* n2) {
for (; n1 && n2; n1 = n1->next, n2 = n2->next)
if (n1->data != n2->data)
return 0;
// returning 1 if data at all nodes are equal.
return 1;
}
// Function to check whether the list is palindrome
int isPalindrome(struct Node* head) {
if (!head || !head->next)
return 1;
struct Node *slow = head, *fast = head;
while (fast->next && fast->next->next) {
slow = slow->next;
fast = fast->next->next;
}
struct Node* head2 = reverse(slow->next);
slow->next = NULL;
int ret = isIdentical(head, head2);
head2 = reverse(head2);
slow->next = head2;
return ret;
}
struct Node* createNode(int data) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
int main() {
// Linked list : 1->2->3->2->1
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(2);
head->next->next->next->next = createNode(1);
int result = isPalindrome(head);
if (result)
printf("true\n");
else
printf("false\n");
return 0;
}
Java
// Java program to check if linked list is palindrome
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
// Class to check if the linked list is palindrome or not
class GfG {
// Function to reverse a linked list
static Node reverseList(Node head) {
Node prev = null;
Node curr = head;
Node next;
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
// Function to check if two lists are identical
static boolean isIdentical(Node n1, Node n2) {
while (n1 != null && n2 != null) {
if (n1.data != n2.data)
return false;
n1 = n1.next;
n2 = n2.next;
}
return true;
}
// Function to check whether the list is palindrome
static boolean isPalindrome(Node head) {
if (head == null || head.next == null)
return true;
Node slow = head, fast = head;
while (fast.next != null
&& fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
Node head2 = reverseList(slow.next);
slow.next = null;
boolean ret = isIdentical(head, head2);
head2 = reverseList(head2);
slow.next = head2;
return ret;
}
public static void main(String[] args) {
// Linked list : 1->2->3->2->1
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(2);
head.next.next.next.next = new Node(1);
boolean result = isPalindrome(head);
if (result)
System.out.println("true");
else
System.out.println("false");
}
}
Python
# Python3 program to check if
# linked list is palindrome
class Node:
def __init__(self, d):
self.data = d
self.next = None
# Function to reverse a linked list
def reverse(head):
prev = None
curr = head
while curr:
next_node = curr.next
curr.next = prev
prev = curr
curr = next_node
return prev
# Function to check if two lists are identical
def isIdentical(n1, n2):
while n1 and n2:
if n1.data != n2.data:
return False
n1 = n1.next
n2 = n2.next
return True
# Function to check whether the list is palindrome
def isPalindrome(head):
if head is None or head.next is None:
return True
slow, fast = head, head
# Find the middle of the list
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
# Split the list and reverse the second half
head2 = reverse(slow.next)
slow.next = None
# Check if the two halves are identical
ret = isIdentical(head, head2)
# Restore the original list
head2 = reverse(head2)
slow.next = head2
return ret
if __name__ == "__main__":
# Linked list : 1->2->3->2->1
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(2)
head.next.next.next.next = Node(1)
result = isPalindrome(head)
if result:
print("true")
else:
print("false")
C#
// C# program to check if linked list is palindrome
using System;
class Node {
public int Data;
public Node Next;
public Node(int d) {
Data = d;
Next = null;
}
}
class GfG {
// Function to reverse a linked list
static Node ReverseList(Node head) {
Node prev = null;
Node curr = head;
while (curr != null) {
Node next = curr.Next;
curr.Next = prev;
prev = curr;
curr = next;
}
return prev;
}
// Function to check if two lists are identical
static bool IsIdentical(Node n1, Node n2) {
while (n1 != null && n2 != null) {
if (n1.Data != n2.Data)
return false;
n1 = n1.Next;
n2 = n2.Next;
}
return true;
}
// Function to check whether the list is palindrome
static bool IsPalindrome(Node head) {
if (head == null || head.Next == null) return true;
Node slow = head, fast = head;
// Find the middle of the list
while (fast.Next != null
&& fast.Next.Next != null) {
slow = slow.Next;
fast = fast.Next.Next;
}
// Split the list and reverse the second half
Node head2 = ReverseList(slow.Next);
slow.Next = null;
// Check if the two halves are identical
bool ret = IsIdentical(head, head2);
// Restore the original list
head2 = ReverseList(head2);
slow.Next = head2;
return ret;
}
static void Main() {
// Linked list : 1->2->3->2->1
Node head = new Node(1);
head.Next = new Node(2);
head.Next.Next = new Node(3);
head.Next.Next.Next = new Node(2);
head.Next.Next.Next.Next = new Node(1);
bool result = IsPalindrome(head);
Console.WriteLine(result ? "true" : "false");
}
}
JavaScript
// Javascript program to check if linked
// list is palindrome
class Node {
constructor(d) {
this.data = d;
this.next = null;
}
}
// Function to reverse a linked list
function reverseList(head) {
let prev = null;
let curr = head;
while (curr) {
let next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
// Function to check if two lists are identical
function isIdentical(n1, n2) {
while (n1 && n2) {
if (n1.data !== n2.data) {
return false;
}
n1 = n1.next;
n2 = n2.next;
}
return true;
}
// Function to check whether the list is palindrome
function isPalindrome(head) {
if (head === null || head.next === null) {
return true;
}
// Initialize slow and fast pointers
let slow = head;
let fast = head;
// Find the middle of the linked list
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
}
// Reverse the second half of the list
let head2 = reverseList(slow);
// Check if the two halves are identical
let ret = isIdentical(head, head2);
// Restore the original list
reverseList(head2);
return ret;
}
// Linked list : 1->2->3->2->1
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(2);
head.next.next.next.next = new Node(1);
let result = isPalindrome(head);
if (result) {
console.log("true");
} else {
console.log("false");
}
Similar Reads
Multilevel Linked List
Multilevel Linked ListMultilevel Linked List is a 2D data structure that comprises several linked lists and each node in a multilevel linked list has a next and child pointer. All the elements are linked using pointers. multilevel linked list Representation:A multilevel linked list is represented by
8 min read
Types of Linked List
A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. In simple words, a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the
15+ min read
Singly Linked List Problems
Singly linked list is a linear data structure in which the elements are not stored in contiguous memory locations and each element is connected only to its next element using a pointer.Learn Basics of Singly Linked List:Basic Terminologies in Linked ListSingly Linked List TutorialLinked List vs Arra
3 min read
Reverse a Linked List
Given a linked list, the task is to reverse the linked list by changing the links between nodes.Examples: Input: head: 1 -> 2 -> 3 -> 4 -> NULLOutput: head: 4 -> 3 -> 2 -> 1 -> NULLExplanation: Reversed Linked List: Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> NULLOut
15+ min read
Orthogonal Linked List
An Orthogonal Linked List is a data structure composed of fundamental elements called Nodes (similar to linked lists). Each node in an orthogonal Linked List points to 4 other nodes, namely up, down, left and right. In essence, just like a matrix is a 2D version of an array, an orthogonal linked lis
9 min read
Linked List meaning in DSA
A linked list is a linear data structure used for storing a sequence of elements, where each element is stored in a node that contains both the element and a pointer to the next node in the sequence. Linked ListTypes of linked lists: Linked lists can be classified in the following categories Singly
4 min read
Linked List of Linked List
Linked list of linkeÂd list, also known as nested linkeÂd lists, is a type of Linked List where each main node stores another full linked list. This structure beats old, plain linked lists. It gives way more flexibility to store and manage compleÂx data. In this article we will learn about the bas
9 min read
Check linked list with a loop is palindrome or not
Given a linked list with a loop, the task is to find whether it is palindrome or not. You are not allowed to remove the loop. Examples: Input : 1 -> 2 -> 3 -> 2 /|\ \|/ ------- 1 Output: Palindrome Linked list is 1 2 3 2 1 which is a palindrome. Input : 1 -> 2 -> 3 -> 4 /|\ \|/ ---
14 min read
Singly Linked List Tutorial
A singly linked list is a fundamental data structure, it consists of nodes where each node contains a data field and a reference to the next node in the linked list. The next of the last node is null, indicating the end of the list. Linked Lists support efficient insertion and deletion operations.Un
8 min read
Traversal of Singly Linked List
Traversal of Singly Linked List is one of the fundamental operations, where we traverse or visit each node of the linked list. In this article, we will cover how to traverse all the nodes of a singly linked list along with its implementation. Examples:Input: 1->2->3->4->5->nullOutput:
11 min read