Given the head of a singly linked list, return true if it is a palindrome or false otherwise.
Example 1:
Input: head = [1,2,2,1] Output: true
Example 2:
Input: head = [1,2] Output: false
Constraints:
[1, 105].0 <= Node.val <= 9O(n) time and O(1) space?The key idea for solving Palindrome Linked List is to compare elements from the beginning and the end of the list without converting the list into another structure unnecessarily. A common approach uses the two-pointer technique. By moving a slow pointer one step and a fast pointer two steps at a time, you can locate the middle of the linked list. Once the midpoint is found, reverse the second half of the list and compare it node-by-node with the first half to determine if the values match.
An alternative approach is to use a stack to store the first half of the elements while traversing the list, then compare them with the second half as you continue traversal. A recursive strategy can also simulate comparing nodes from both ends by leveraging the call stack.
The optimal method runs in O(n) time and uses O(1) additional space when the second half of the list is reversed in-place.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Two Pointers + Reverse Second Half | O(n) | O(1) |
| Stack-Based Comparison | O(n) | O(n) |
| Recursive Comparison | O(n) | O(n) |
take U forward
This approach involves reversing the second half of the linked list and then comparing it with the first half. If they are identical, the linked list is a palindrome. The steps include:
Time Complexity: O(n) since we are traversing the list multiple times but each in linear time.
Space Complexity: O(1) as we are reversing the linked list in place.
1#include <iostream>
2using namespace std;
3
4struct ListNode {
5 int val;
6 ListNode *next;
7 ListNode(int x) : val(x), next(NULL) {}
8};
9
10ListNode* reverseList(ListNode* head) {
11 ListNode* prev = NULL;
12 ListNode* current = head;
13 while (current != NULL) {
14 ListNode* nextNode = current->next;
current->next = prev;
prev = current;
current = nextNode;
}
return prev;
}
bool isPalindrome(ListNode* head) {
if (head == NULL || head->next == NULL) return true;
ListNode* slow = head;
ListNode* fast = head;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
ListNode* second_half = reverseList(slow);
ListNode* first_half = head;
while (second_half != NULL) {
if (first_half->val != second_half->val) return false;
first_half = first_half->next;
second_half = second_half->next;
}
return true;
}In this C++ solution, we also reverse the second half of the list and compare it with the first half. The use of pointers allows us to efficiently manage the linked list nodes within the constraints provided.
In this approach, convert the linked list into an array and utilize a two-pointer technique to determine if it forms a palindrome. The steps are as follows:
Time Complexity: O(n) due to single traversation and subsequent O(n/2) check.
Space Complexity: O(n) because we store all node values in an array.
using System.Collections.Generic;
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
}
public class Solution {
public bool IsPalindrome(ListNode head) {
List<int> values = new List<int>();
ListNode current = head;
while (current != null) {
values.Add(current.val);
current = current.next;
}
int left = 0, right = values.Count - 1;
while (left < right) {
if (values[left] != values[right]) return false;
left++;
right--;
}
return true;
}
}Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, Palindrome Linked List is a common interview question in companies like Amazon, Google, and Meta. It tests understanding of linked list traversal, two-pointer techniques, and in-place list manipulation.
Yes, recursion can be used to traverse to the end of the list and compare nodes while the call stack unwinds. Although elegant, this approach uses O(n) stack space, which is less optimal than the in-place iterative method.
The optimal approach uses the two-pointer technique to find the middle of the linked list, then reverses the second half and compares it with the first half. This method runs in O(n) time and uses O(1) extra space, making it ideal for interviews.
A stack can be used to store values from the first half of the linked list and compare them with the second half during traversal. However, the most efficient solution modifies the list temporarily by reversing its second half instead of using extra memory.
C# List is employed to store linked list values before a two-pointer check thoroughly verifies if the stored sequence forms a palindrome.