Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.
METHOD 1 (Use a Stack)
A simple solution is to use a stack of list nodes. This mainly involves three steps.
Traverse the given list from head to tail and push every visited node to stack.
Traverse the list again. For every visited node, pop a node from the stack and compare data of popped node with the currently visited node.
If all nodes matched, then return true, else false.
Below image is a dry run of the above approach:
Below is the implementation of the above approach :
C++
#include
using namespace std;
class Node {
public:
int data;
Node(int d){
data = d;
}
Node *ptr;
};
// Function to check if the linked list
// is palindrome or not
bool isPalin(Node* head){
// Temp pointer
Node* slow= head;
// Declare a stack
stack s;
// Push all elements of the list
// to the stack
while(slow != NULL){
s.push(slow->data);
// Move ahead
slow = slow->ptr;
}
// Iterate in the list again and
// check by popping from the stack
while(head != NULL ){
// Get the top most element
int i=s.top();
// Pop the element
s.pop();
// Check if data is not
// same as popped element
if(head -> data != i){
return false;
}
// Move ahead
head=head->ptr;
}
return true;
}
// Driver Code
int main(){
// Addition of linked list
Node one = Node(1);
Node two = Node(2);
Node three = Node(3);
Node four = Node(2);
Node five = Node(1);
// Initialize the next pointer
// of every current pointer
five.ptr = NULL;
one.ptr = &two;
two.ptr = &three;
three.ptr = &four;
four.ptr = &five;
Node* temp = &one;
// Call function to check palindrome or not
int result = isPalin(&one);
if(result == 1)
cout<<"isPalindrome is true\n";
else
cout<<"isPalindrome is true\n";
return 0;
}
// This code has been contributed by Striver
Java
/* Java program to check if linked list is palindrome recursively */
import java.util.*;
class linkeList {
public static void main(String args[])
{
Node one = new Node(1);
Node two = new Node(2);
Node three = new Node(3);
Node four = new Node(4);
Node five = new Node(3);
Node six = new Node(2);
Node seven = new Node(1);
one.ptr = two;
two.ptr = three;
three.ptr = four;
four.ptr = five;
five.ptr = six;
six.ptr = seven;
boolean condition = isPalindrome(one);
System.out.println("isPalidrome :" + condition);
}
static boolean isPalindrome(Node head)
{
Node slow = head;
boolean ispalin = true;
Stack stack = new Stack();
while (slow != null) {
stack.push(slow.data);
slow = slow.ptr;
}
while (head != null) {
int i = stack.pop();
if (head.data == i) {
ispalin = true;
}
else {
ispalin = false;
break;
}
head = head.ptr;
}
return ispalin;
}
}
class Node {
int data;
Node ptr;
Node(int d)
{
ptr = null;
data = d;
}
}
Python3
# Python3 program to check if linked
# list is palindrome using stack
class Node:
def __init__(self,data):
self.data = data
self.ptr = None
# Function to check if the linked list
# is palindrome or not
def ispalindrome(head):
# Temp pointer
slow = head
# Declare a stack
stack = []
ispalin = True
# Push all elements of the list
# to the stack
while slow != None:
stack.append(slow.data)
# Move ahead
slow = slow.ptr
# Iterate in the list again and
# check by popping from the stack
while head != None:
# Get the top most element
i = stack.pop()
# Check if data is not
# same as popped element
if head.data == i:
ispalin = True
else:
ispalin = False
break
# Move ahead
head = head.ptr
return ispalin
# Driver Code
# Addition of linked list
one = Node(1)
two = Node(2)
three = Node(3)
four = Node(4)
five = Node(3)
six = Node(2)
seven = Node(1)
# Initialize the next pointer
# of every current pointer
one.ptr = two
two.ptr = three
three.ptr = four
four.ptr = five
five.ptr = six
six.ptr = seven
seven.ptr = None
# Call function to check palindrome or not
result = ispalindrome(one)
print("isPalindrome:", result)
# This code is contributed by Nishtha Goel
C#
// C# program to check if linked list
// is palindrome recursively
using System;
using System.Collections.Generic;
class linkeList{
// Driver code
public static void Main(String []args)
{
Node one = new Node(1);
Node two = new Node(2);
Node three = new Node(3);
Node four = new Node(4);
Node five = new Node(3);
Node six = new Node(2);
Node seven = new Node(1);
one.ptr = two;
two.ptr = three;
three.ptr = four;
four.ptr = five;
five.ptr = six;
six.ptr = seven;
bool condition = isPalindrome(one);
Console.WriteLine("isPalidrome :" + condition);
}
static bool isPalindrome(Node head)
{
Node slow = head;
bool ispalin = true;
Stack stack = new Stack();
while (slow != null)
{
stack.Push(slow.data);
slow = slow.ptr;
}
while (head != null)
{
int i = stack.Pop();
if (head.data == i)
{
ispalin = true;
}
else
{
ispalin = false;
break;
}
head = head.ptr;
}
return ispalin;
}
}
class Node
{
public int data;
public Node ptr;
public Node(int d)
{
ptr = null;
data = d;
}
}
// This code is contributed by amal kumar choubey
Javascript
/* JavaScript program to check if
linked list is palindrome recursively */
class Node {
constructor(val) {
this.data = val;
this.ptr = null;
}
}
var one = new Node(1);
var two = new Node(2);
var three = new Node(3);
var four = new Node(4);
var five = new Node(3);
var six = new Node(2);
var seven = new Node(1);
one.ptr = two;
two.ptr = three;
three.ptr = four;
four.ptr = five;
five.ptr = six;
six.ptr = seven;
var condition = isPalindrome(one);
document.write("isPalidrome: " + condition);
function isPalindrome(head) {
var slow = head;
var ispalin = true;
var stack = [];
while (slow != null) {
stack.push(slow.data);
slow = slow.ptr;
}
while (head != null) {
var i = stack.pop();
if (head.data == i) {
ispalin = true;
} else {
ispalin = false;
break;
}
head = head.ptr;
}
return ispalin;
}
// This code is contributed by todaysgaurav
Output
isPalindrome: true
Time complexity: O(n).
METHOD 2 (By reversing the list) This method takes O(n) time and O(1) extra space. 1) Get the middle of the linked list. 2) Reverse the second half of the linked list. 3) Check if the first half and second half are identical. 4) Construct the original linked list by reversing the second half again and attaching it back to the first half
To divide the list into two halves, method 2 of this post is used.
When a number of nodes are even, the first and second half contain exactly half nodes. The challenging thing in this method is to handle the case when the number of nodes is odd. We don’t want the middle node as part of the lists as we are going to compare them for equality. For odd cases, we use a separate variable ‘midnode’.
C++
// C++ program to check if a linked list is palindrome
/* javascript program to check if linked list is palindrome */
var head; // head of list
var slow_ptr, fast_ptr, second_half;
/* Linked list Node */
class Node {
constructor(val) {
this.data = val;
this.next = null;
}
}
/*
* Function to check if given linked list is palindrome or not
*/
function isPalindrome(head) {
slow_ptr = head;
fast_ptr = head;
var prev_of_slow_ptr = head;
var midnode = null; // To handle odd size list
var res = true; // initialize result
if (head != null && head.next != null) {
/*
* Get the middle of the list. Move slow_ptr by 1 and fast_ptr by 2, slow_ptr
* will have the middle node
*/
while (fast_ptr != null && fast_ptr.next != null) {
fast_ptr = fast_ptr.next.next;
/*
* We need previous of the slow_ptr for linked lists with odd elements
*/
prev_of_slow_ptr = slow_ptr;
slow_ptr = slow_ptr.next;
}
/*
* fast_ptr would become NULL when there are even elements in the list and not
* NULL for odd elements. We need to skip the middle node for odd case and store
* it somewhere so that we can restore the original list
*/
if (fast_ptr != null) {
midnode = slow_ptr;
slow_ptr = slow_ptr.next;
}
// Now reverse the second half and compare it with first half
second_half = slow_ptr;
prev_of_slow_ptr.next = null; // NULL terminate first half
reverse(); // Reverse the second half
res = compareLists(head, second_half); // compare
/* Construct the original list back */
reverse(); // Reverse the second half again
if (midnode != null) {
// If there was a mid node (odd size case) which
// was not part of either first half or second half.
prev_of_slow_ptr.next = midnode;
midnode.next = second_half;
} else
prev_of_slow_ptr.next = second_half;
}
return res;
}
/*
* Function to reverse the linked list Note that this function may change the
* head
*/
function reverse() {
var prev = null;
var current = second_half;
var next;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
second_half = prev;
}
/* Function to check if two input lists have same data */
function compareLists(head1, head2) {
var temp1 = head1;
var temp2 = head2;
while (temp1 != null && temp2 != null) {
if (temp1.data == temp2.data) {
temp1 = temp1.next;
temp2 = temp2.next;
} else
return false;
}
/* Both are empty return 1 */
if (temp1 == null && temp2 == null)
return true;
/*
* Will reach here when one is NULL and other is not
*/
return false;
}
/*
* Push a node to linked list. Note that this function changes the head
*/
function push( new_data) {
/*
* Allocate the Node & Put in the data
*/
var new_node = new Node(new_data);
/* link the old list off the new one */
new_node.next = head;
/* Move the head to point to new Node */
head = new_node;
}
// A utility function to print a given linked list
function printList(ptr) {
while (ptr != null) {
document.write(ptr.data + "->");
ptr = ptr.next;
}
document.write("NULL ");
}
/* Driver program to test the above functions */
/* Start with the empty list */
var str = [ 'a', 'b', 'a', 'c', 'a', 'b', 'a' ];
var string = str.toString();
for (i = 0; i < 7; i++) {
push(str[i]);
printList(head);
if (isPalindrome(head) != false) {
document.write("Is Palindrome");
document.write(" ");
} else {
document.write("Not Palindrome");
document.write(" ");
}
}
// This code contributed by gauravrajput1
Output:
a->NULL
Is Palindrome
b->a->NULL
Not Palindrome
a->b->a->NULL
Is Palindrome
c->a->b->a->NULL
Not Palindrome
a->c->a->b->a->NULL
Not Palindrome
b->a->c->a->b->a->NULL
Not Palindrome
a->b->a->c->a->b->a->NULL
Is Palindrome
Time Complexity: O(n) Auxiliary Space: O(1)
METHOD 3 (Using Recursion) Use two pointers left and right. Move right and left using recursion and check for following in each recursive call. 1) Sub-list is a palindrome. 2) Value at current left and right are matching.
If both above conditions are true then return true.
The idea is to use function call stack as a container. Recursively traverse till the end of list. When we return from last NULL, we will be at the last node. The last node to be compared with first node of list.
In order to access first node of list, we need list head to be available in the last call of recursion. Hence, we pass head also to the recursive function. If they both match we need to compare (2, n-2) nodes. Again when recursion falls back to (n-2)nd node, we need reference to 2nd node from the head. We advance the head pointer in the previous call, to refer to the next node in the list. However, the trick is identifying a double-pointer. Passing a single pointer is as good as pass-by-value, and we will pass the same pointer again and again. We need to pass the address of the head pointer for reflecting the changes in parent recursive calls. Thanks to Sharad Chandra for suggesting this approach.
C++
// Recursive program to check if a given linked list is palindrome
#include
using namespace std;
/* Link list node */
struct node {
char data;
struct node* next;
};
// Initial parameters to this function are &head and head
a->NULL
Not Palindrome
b->a->NULL
Not Palindrome
a->b->a->NULL
Is Palindrome
c->a->b->a->NULL
Not Palindrome
a->c->a->b->a->NULL
Not Palindrome
b->a->c->a->b->a->NULL
Not Palindrome
a->b->a->c->a->b->a->NULL
Is Palindrome
Time Complexity: O(n) Auxiliary Space: O(n) if Function Call Stack size is considered, otherwise O(1).
Article Tags :
Linked List
Accolite
Adobe
Amazon
KLA Tencor
Kritikal Solutions
Microsoft
palindrome
Snapdeal
Yodlee Infotech
Practice Tags :
Accolite
Amazon
Microsoft
Snapdeal
Adobe
Yodlee Infotech
KLA Tencor
Kritikal Solutions
Linked List
palindrome
Read Full Article
Program to determine whether a singly linked list is the palindrome
Explanation
In this program, we need to check whether given singly linked list is a palindrome or not. A palindromic list is the one which is equivalent to the reverse of itself.
The list given in the above figure is a palindrome since it is equivalent to its reverse list, i.e., 1, 2, 3, 2, 1. To check whether a list is a palindrome, we traverse the list and check if any element from the starting half doesn't match with any element from the ending half, then we set the variable flag to false and break the loop.
In the last, if the flag is false, then the list is palindrome otherwise not. The algorithm to check whether a list is a palindrome or not is given below.
Algorithm
Create a class Node which has two attributes: data and next. Next is a pointer to the next node in the list.
Create another class Palindrome which has three attributes: head, tail, and size.
addNode() will add a new node to the list:
Create a new node.
It first checks, whether the head is equal to null which means the list is empty.
If the list is empty, both head and tail will point to a newly added node.
If the list is not empty, the new node will be added to end of the list such that tail's next will point to a newly added node. This new node will become the new tail of the list.
reverseList() will reverse the order of the node present in the list:
Node current will represent a node from which a list needs to be reversed.
Node prevNode represent the previous node to current and nextNode represent the node next to current.
The list will be reversed by swapping the prevNode with nextNode for each node.
isPalindrome() will check whether given list is palindrome or not:
Declare a node current which will initially point to head node.
The variable flag will store a boolean value true.
Calculate the mid-point of the list by dividing the size of the list by 2.
Traverse through the list till current points to the middle node.
Reverse the list after the middle node until the last node using reverseList(). This list will be the second half of the list.
Now, compare nodes of first half and second half of the list.
If any of the nodes don't match then, set a flag to false and break the loop.
If the flag is true after the loop which denotes that list is a palindrome.
If the flag is false, then the list is not a palindrome.
display() will display the nodes present in the list:
Define a node current which will initially point to the head of the list.
Traverse through the list till current points to null.
Display each node by making current to point to node next to it in each iteration.
Solution
Python
Output:
Nodes of the singly linked list:
1 2 3 2 1
Given singly linked list is a palindrome
C
Output:
Nodes of the singly linked list:
1 2 3 2 1
Given singly linked list is a palindrome
JAVA
Output:
Nodes of singly linked list:
1 2 3 2 1
Given singly linked list is a palindrome
C#
Output:
Nodes of singly linked list:
1 2 3 2 1
Given singly linked list is a palindrome
PHP
Output:
Nodes of singly linked list:
1 2 3 2 1
Given singly linked list is a palindrome
Using Stack:
Algorithm
Traverse the linked list and push the value in Stack.
Now, again traverse the linked list and while doing so pop the values from the stack.If stack become empty it means given Linked list is pallindrom otherwise not.
Given below is a Singly Linked List:
Traversing the list:
Step 1:
Currently,pointer is at the beginning of the list which points to Node value 2
Step 2:
Now,After the Node 2 is visited pointer moves forward and the visited node will be push into the stack.
Step 3:
Node 3 is visited,push Node 3 into Stack and moves the pointer forward.
Step 4:
Node 3 is visited,push Node 3 into Stack and moves the pointer forward.
Step 5:
Node 2 is visited,push Node 2 into Stack and moves the pointer forward until it's NULL.
Step 6:
Now,our approach will be to traverse the list once again from the beginning and this time will be ke checking is the Stack.top() is equal to the current node or not.Here,top value of stack is 2 and current node value of list is 2 they both are equal then pop the top value from the stack and move the pointer forward,repeat these steps until list found to be NULL. Whenever you found that top value of stack is not equal to the current value of node then return false.
Step 7:
Step 8:
Step 9:
Step 10:
The following code implements the above algorithm:
#include
using namespace std;
typedef struct Node {
int data;
struct Node * next;
} Node;
Node * createNode(int val){
Node * temp = (Node *)malloc(sizeof(Node));
if(temp){
temp->data = val;
temp->next = NULL;
}
return temp;
}
/* This function inserts node at the head of linked list */
void push(Node **headRef, int data){
Node * newNode = createNode(data);
newNode->next = *headRef;
*headRef = newNode;
}
void printList(Node *head){
while(head){
cout<data<<"->";
head = head->next;
}
cout<<"Null";
}
bool isPalindrome(Node *head)
{
Node* temp=head;
//Declare a Stack
stacks;
//Push all the values of list into the stack
while(temp)
{
s.push(temp->data);
temp=temp->next;
}
while(head != NULL ){
// Get the top most element
int val=s.top();
// Pop the element
s.pop();
//Check is the data in the stack and list is same or not
if(head -> data != val){
return false;
}
// Move ahead
head=head->next;
}
return true;
}
int main()
{
Node *head = NULL;
push(&head, 2);
push(&head, 3);
push(&head, 3);
push(&head, 2);
cout<<"Original list :";
printList(head);
//function to Check list is Palindrome or not
if(isPalindrome(head))cout<<"\nGiven List is Palindrome";
else cout<<"\nGiven List is not Palindrome";
}
Time Complexity is O(n) and Space Complexity is O(n)
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include
#include
// A Linked List Node
structNode
{
intdata;
structNode*next;
};
// Helper function to create a new node with the given data and
// Function to check if the linked list is a palindrome or not
intcheckPalin(structNode*head){
returncheckPalindrome(&head,head);
}
intmain(void)
{
// input keys
intkeys[]={1,3,5,3,1};
intn=sizeof(keys)/sizeof(keys[0]);
structNode*head=NULL;
for(inti=n-1;i>=0;i--){
push(&head,keys[i]);
}
if(checkPalin(head)){
printf("The linked list is a palindrome");
}
else{
printf("The linked list is not a palindrome");
}
return0;
}
DownloadRun Code
Output:
The linked list is a palindrome
Check if a Linked List is Palindrome (C++ Code)
Every engineering student aspires to get a decent placement, but the journey to that goal isn’t very easy for most of us, don’t worry in this tutorial we are going to take a little load off your shoulders and discuss one of the most frequently asked interview questions on the topic linked list. This particular problem statement has been asked in technical interviews of many multinational companies like Google, Facebook, and Uber. Let’s understand first what exactly is a palindrome.
Check if a Singly Linked List is a Palindrome-Interview Problem
Difficulty: Medium
Asked in: Amazon, Microsoft, Adobe, Flipkart
Understanding the Problem:
Given a singly Linked List, you have to find out whether the linked list is a palindrome or not. A palindrome is nothing but a string or number which is same whether you read it from right to left or left to right. It is like the string and its mirror image both are same.
For example,
Input: 1->2->2->1->NULL
Output: Yes
Input: 7->7->7->NULL
Output: Yes
Input: 1->2->3->NULL
Output: No
Possible follow-up questions to ask the interviewer: →
Is the data value of the given Linked List is only numbers? (Ans: No, the data values can be anything.)
Linked List’s Node Class
class ListNode
{
int data
ListNode next
}
Solutions
We are going to discuss three different ways to approach this problem. Also, we will be writing the pseudo-codes for all the three possible solutions.
Solution Using Stack: Here we will be using the stack data structure. Stack has a property that the element inserted first will be taken out last. This property will help us to find whether the given Linked List is palindrome or not.
Solution By Reversing the Second Half: By reversing the second half, and making a little observation we can solve this problem easily.
Solution with Recursion: We will here make use of recursion stack in a much smarter way to find our solution.
Solution Using Stack
Solution idea
The idea is very simple and straight-forward, we all know that stack is based on FILO(First In Last Out) principle. We will keep pushing all the elements of Linked List into the stack and once the linked list is fully traversed we will start popping the elements out of the stack and comparing with the linked list’s elements.
For better understanding the approach, let’s see an explanation: →
Given Linked List is : 1->2->2->1->NULL
Initially, our stack S is empty, we will start pushing elements of the linked list one after the other:
S = 1 (pushing 1)
S = 1 2 (pushing 2)
S = 1 2 2 (pushing 2)
S = 1 2 2 1 (pushing 1)
Now we will start popping an element out of the stack and comparing it with the linked list’s data starting from the head.
Similarly, if all the elements from both stack and linked list get matched as in this case, we can conclude that the linked list is a palindrome.
Solution steps
We will create a stack S and push all the elements of the Linked List into the stack.
Then we will pop the elements one by one from the stack and compare it with the data present in the Linked List sequentially.
If there is any mismatch in comparison, we can conclude that the Linked List is not a palindrome else it is a palindrome.
Pseudo-code
boolean isListPalindrome(ListNode head)
{
// if the linked list is empty or having only one node
if(head == NULL or head.next == NULL)
return True
stack S
ListNode temp = head
// traversing the linked list and pushing the elements into stack
while(temp is not NULL)
{
S.push(temp.data)
temp=temp.next
}
temp = head
// again traversing the linked list to compare
while(temp is not NULL)
{
topOfStack = S.top()
S.pop()
if(topOfStack is not equal temp)
return False
temp=temp.next
}
return True
}
Complexity Analysis
Time Complexity: O(L), where L is the length of the given linked list.
Space Complexity: How much extra space used? (Think!)
Critical ideas to think!
Take an example of palindrome, divide the palindrome into two equal parts. Reverse the second half of the palindrome. Have you got any observation?
Solution By Reversing the Second Half
Solution idea
We will divide our Linked List into two halves by finding the middle node. When we reverse the second half of the Linked List and if the Linked List is a palindrome then the second half becomes exactly same to the first half of the Linked List. This solution is based on this idea.
Solution steps
We will first find the middle node of the given Linked List(Consider both the odd and even cases).
Then, we will reverse the second half of the Linked List.
We will compare the second half with the first half, if both the halves are exactly the same then the linked list is a palindrome.
Reconstruct the actual given Linked List by again reversing the second half and attaching it to the first half.
Pseudo-code
boolean compareList(firstHalf , secondHalf)
{
while(firstHalf is not NULL and secondHalf is not NULL)
{
if(firstHalf.data is not equal secondHalf.data)
return False
firstHalf = firstHalf.next
secondHalf = secondHalf.next
}
return True
}
ListNode reverseList(ListNode head)
{
if(head is NULL or head.next is NULL)
return head
ListNode reversedPart = reverseList(head.next)
head.next.next = head
head.next = NULL
return reversedPart
}
boolean isListPalindrome(ListNode head)
{
ListNode midNode = NULL
ListNode slow_pointer = head
ListNode previous_of_mid = NULL
ListNode fast_pointer = head.next
if(head is not NULL and head.next is not NULL)
{
while(fast_pointer is not NULL and fast_pointer.next is not NULL)
{
fast_pointer = fast_pointer.next.next
previous_of_mid = slow_pointer
slow_pointer = slow_pointer.next
}
if(fast_pointer is not NULL) //If there are odd nodes
{
midNode = slow_pointer
slow_pointer = slow_pointer.next
}
ListNode secondHalf = slow_pointer
ListNode reversedHalf = reverse(secondHalf)
boolean result = compareList(head, reversedHalf)
}
}
Complexity Analysis
Time Complexity: O(N), where N is the length of the Linked List
Space Complexity: O(N) (Why? Can you reduce this complexity?)
Critical ideas to think!
You must have got the idea of how to reduce the space complexity(Think if not). Learn more about the algorithm used here to find the middle node.
Solution Using Recursion
Solution idea
This solution uses the recursion call stack in a very efficient way. We will take two pointers, one for the leftmost node and the other one for the rightmost node. We will check if the data of the left and the right nodes matches or not. If there is a match, we will go on recursively to check for the rest of the list. At last, if all the calls return true (that conveys match), we can conclude that the given Linked List is a palindrome.
Solution steps
Recursively traverse the entire linked list to get the last node as a rightmost node.
Compare the first node (left node) with the right node. If both are having same data value then recursively call for the sub-list as to check if the sub-list is a palindrome or not.
If all the recursive calls are returning true, it means the Linked List given is a palindrome else it is not a palindrome.
Pseudo-code
boolean isListPalindrome(ListNode &leftmostNode, ListNode rightmostNode)
{
if(rightmostNode == NULL)
return True
//check if the sub-list is a palindrome
boolean resultSublist = isListPalindrome(leftmostNode, rightmostNode.next)
if(resultSublist is False) //No need to check then
return False
boolean result = leftmostNode.data == rightmostNode.data
leftmostNode = leftmostNode.next
return result
}
Complexity Analysis
Time Complexity: O(N)
Space Complexity: O(N) (Stack space)
Critical ideas to think!
Try to understand the recursion call stack for this solution and getting a clear understanding of how recursion works.
Here we have passed the left node pointer variable as a reference. Why?
Comparison of different Solutions
Suggested Problems to Solve
Sum of the values present in the nodes of the Linked List.
Convert a Linked List into an array.
Check if the given Linked List is sorted or not.
Convert a Circular Linked List into a Singly Linked List.
Happy Coding! Enjoy Algorithms!!
Problem Statement
In the “Check if a Linked list of Strings form a Palindrome” problem we have given a linked list handling string data. Write a program to check whether the data forms a palindrom or not.
Example
ba->c->d->ca->b1
Explanation: In the above example we can see that the string “bacdcab” is a palindrome
Approach
Given a linked list in which each node contains a string. We have to check if the data in the linked list form a palindrome. A string is a palindrome if it reads the same forwards as it does backward. For example, the number “bacdcab” is a palindrome. A linked list forms palindromes if they have the same order of elements when traversed forwards and backward.
C
/* Program to check if a linked list is palindrome */