AfterAcademy Tech
•
10 Feb 2020

Difficulty: Medium
Asked in: Amazon, Microsoft, Adobe, Flipkart
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: →
Linked List’s Node Class
class ListNode
{
int data
ListNode next
}
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 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.
1(S.top) == 1(Linked List’s head)
2(S.top) == 2(Linked List’s head.next)
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
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 <List Datatype> 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!
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
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!
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
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!

Happy Coding! Enjoy Algorithms!!
AfterAcademy Tech
This is an interview problem asked by companies like Myntra, Visa, Oracle, Adobe, etc. The problem is to remove the duplicate nodes from the given Linked List so that only unique nodes(with unique data values) are present.

AfterAcademy Tech
Sort a linked list using insertion sort and return its head.

AfterAcademy Tech
In this blog, we will discuss the types of linked list and basic operations that can be performed on a linked list.

AfterAcademy Tech
This is an interview problem asked in companies like Amazon and Microsoft. Given a Linked List you have to swap nodes of the list in pairs.
