Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Linked List Palindrome Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Linked List Palindrome

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a non-negative number represented as a linked list, determine whether it is a palindrome or not.

Approach

The idea is to compare the two halves of the linked list.

  • Get the middle element of the linked list
  • Reverse the second half of the linked list and compare it with the first half.
  • If both the halves are equal then return true otherwise return false.

Analysis

  • Time Complexity: O(n)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
/* This is the ListNode class definition

class ListNode {
public:
	int data;
	ListNode* next;

	ListNode(int data) {
		this->data = data;
		this->next = NULL;
	}
};
*/
ListNode* getMiddleNode(ListNode* head) {
	ListNode* front = head;
	while (front->next != NULL && front->next->next != NULL) {
		head = head->next;
		front = front->next->next;
	} 
	return head;
}
ListNode* reverse (ListNode *head) {
	ListNode *prevNode = NULL;
	ListNode *nextNode;
	while (head->next != NULL) {
		nextNode = head->next;
		head->next = prevNode;
		prevNode = head;
		head = nextNode;
	} 
	head->next = prevNode;
	return head;
}
bool isPalindrome(ListNode* list) {
    ListNode* middleNode = getMiddleNode(list);
	ListNode *secondHalf = reverse(middleNode->next);
	while(secondHalf != NULL) {
		if(secondHalf->data != list->data) {
			return false;
		}
		secondHalf = secondHalf->next;
		list = list->next;
	}
	return true;
}
Java
/** This is the ListNode class definition

class ListNode {
	int data;
	ListNode next;

	ListNode(int data) {
		this.data = data;
		this.next = null;
	}
}
**/
class Solution {
	ListNode getMiddleNode(ListNode head) {
		ListNode front = head;
		while (front.next != null && front.next.next != null) {
			head = head.next;
			front = front.next.next;
		} 
		return head;
	}
	ListNode reverse (ListNode head) {
		ListNode prevNode = null;
		ListNode nextNode;
		while (head.next != null) {
			nextNode = head.next;
			head.next = prevNode;
			prevNode = head;
			head = nextNode;
		} 
		head.next = prevNode;
		return head;
	}
	boolean isPalindrome(ListNode list) {
	    ListNode middleNode = getMiddleNode(list);
		ListNode secondHalf = reverse(middleNode.next);
		while(secondHalf != null) {
			if(secondHalf.data != list.data) {
				return false;
			}
			secondHalf = secondHalf.next;
			list = list.next;
		}
		return true;
	}
}
Related Content
Add Element at Kth Position in Linked List
Add Two Numbers as Lists
Add One to Linked List
Append Linked Lists
Clone List with Random Pointer
Remove Element at Kth Position in Linked List
Delete Node From Linked List
Delete Xth Node From End of Linked List
Detect Loop in Linked List
Find xth Node from End of Linked List
Flatten a Multi-Level Linked List
Insertion Sort Linked List
Intersection of Two Linked Lists
Kth Element in Linked List
Linked List to Array
Merge Sort Linked List
Merge Two Sorted Linked List
Middle Element of Linked List
Partition List
Print Linked List
Print Reversed Linked List
Remove Duplicates from Sorted Linked List
Remove Duplicates from Sorted Linked List - II
Remove Loop From Linked List
Remove occurrences in Linked List
Reorder List
Reverse a Linked List
Reverse a Linked List II
Reverse a Linked List in k-groups
Rotate a Linked List
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet