234. Palindrome Linked List
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to determine if a given singly linked list is a palindrome. A palindrome is a sequence that reads the same forwards and backwards. For example, the list 1 -> 2 -> 2 -> 1 is a palindrome, while 1 -> 2 is not.
Solution
Palindrome Linked List in Python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
# Base cases: 0 or 1 node, which are palindromes.
if not head or not head.next:
return True
# Step 1: Find the end of the first half.
first_half_end = self.find_first_half_end(head)
# Step 2: Reverse the second half.
second_half_start = self.reverse_list(first_half_end.next)
# Step 3: Compare the first half and the reversed second half.
p1 = head
p2 = second_half_start
result = True
while result and p2:
if p1.val != p2.val:
result = False
p1 = p1.next
p2 = p2.next
# Step 4: Restore the list (optional, good practice).
first_half_end.next = self.reverse_list(second_half_start)
return result
def find_first_half_end(self, head):
fast = head
slow = head
while fast.next and fast.next.next:
fast = fast.next.next
slow = slow.next
return slow
def reverse_list(self, head):
prev = None
curr = head
while curr:
next_temp = curr.next
curr.next = prev
prev = curr
curr = next_temp
return prev
Palindrome Linked List in Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
// Base cases: 0 or 1 node, which are palindromes.
if (head == null || head.next == null) {
return true;
}
// Step 1: Find the end of the first half.
ListNode firstHalfEnd = findFirstHalfEnd(head);
// Step 2: Reverse the second half.
ListNode secondHalfStart = reverseList(firstHalfEnd.next);
// Step 3: Compare the first half and the reversed second half.
ListNode p1 = head;
ListNode p2 = secondHalfStart;
boolean result = true;
while (result && p2 != null) {
if (p1.val != p2.val) {
result = false;
}
p1 = p1.next;
p2 = p2.next;
}
// Step 4: Restore the list (optional, good practice).
firstHalfEnd.next = reverseList(secondHalfStart);
return result;
}
private ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
private ListNode findFirstHalfEnd(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
}
Palindrome Linked List in C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
// Base cases: 0 or 1 node, which are palindromes.
if (!head || !head->next) {
return true;
}
// Step 1: Find the end of the first half.
ListNode* firstHalfEnd = head;
ListNode* fast = head;
while (fast->next != nullptr && fast->next->next != nullptr) {
firstHalfEnd = firstHalfEnd->next;
fast = fast->next->next;
}
// Step 2: Reverse the second half.
ListNode* secondHalfStart = reverseList(firstHalfEnd->next);
// Step 3: Compare the first half and the reversed second half.
ListNode* p1 = head;
ListNode* p2 = secondHalfStart;
bool result = true;
while (result && p2 != nullptr) {
if (p1->val != p2->val) {
result = false;
}
p1 = p1->next;
p2 = p2->next;
}
// Step 4: Restore the list (optional, good practice).
firstHalfEnd->next = reverseList(secondHalfStart);
return result;
}
private:
ListNode* reverseList(ListNode* head) {
ListNode* prev = nullptr;
ListNode* curr = head;
while (curr != nullptr) {
ListNode* nextTemp = curr->next;
curr->next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
};
Palindrome Linked List in TypeScript
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function isPalindrome(head: ListNode | null): boolean {
// Base cases: 0 or 1 node, which are palindromes.
if (head === null || head.next === null) {
return true;
}
// Helper function to reverse a linked list
const reverseList = (node: ListNode | null): ListNode | null => {
let prev: ListNode | null = null;
let curr = node;
while (curr !== null) {
const nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
};
// Helper function to find the end of the first half
const findFirstHalfEnd = (node: ListNode | null): ListNode | null => {
let fast = node;
let slow = node;
while (fast.next !== null && fast.next.next !== null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
};
// Step 1: Find the end of the first half.
const firstHalfEnd = findFirstHalfEnd(head);
// Step 2: Reverse the second half.
const secondHalfStart = reverseList(firstHalfEnd.next);
// Step 3: Compare the first half and the reversed second half.
let p1 = head;
let p2 = secondHalfStart;
let result = true;
while (result && p2 !== null) {
if (p1.val !== p2.val) {
result = false;
}
p1 = p1.next;
p2 = p2.next;
}
// Step 4: Restore the list (optional, good practice).
firstHalfEnd.next = reverseList(secondHalfStart);
return result;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.