# Palindrome Linked List – Leetcode Solution

In this post, we are going to solve the 234. Palindrome Linked List problem of Leetcode. This problem 234. Palindrome Linked List is a Leetcode easy level problem. Let’s see the code, 234. Palindrome Linked List – Leetcode Solution.

## Problem

Given the `head` of a singly linked list, return `true` if it is a palindrome.

### Example 1 :

``````
Output: true
``````

### Example 2 :

``````
Output: false
``````

### Constraints

• The number of nodes in the list is in the range `[1, 105]`.
• `0 <= Node.val <= 9`

Now, let’s see the code of 234. Palindrome Linked List – Leetcode Solution.

# Palindrome Linked List – Leetcode Solution

### 234. Palindrome Linked List – Solution in Java

```/**
* 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 {

ListNode curr = head, prev = null;
while(curr != null){
ListNode forw = curr.next;
curr.next = prev;
prev = curr;
curr = forw;
}
return prev;
}

while(curr.next != null && curr.next.next != null){
curr = curr.next.next;
mid = mid.next;
}
return mid;
}

ListNode halfList = reverse(mid.next);

while(halfList != null){
if(curr.val != halfList.val) return false;
curr = curr.next;
halfList = halfList.next;
}

return true;

}
}```

### 234. Palindrome Linked List – Solution in C++

```/**
* 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:
ListNode* reverse(ListNode* ptr)
{
ListNode* pre=NULL;
ListNode* nex=NULL;
while(ptr!=NULL) {
nex = ptr->next;
ptr->next = pre;
pre=ptr;
ptr=nex;
}
return pre;
}

while(fast->next!=NULL&&fast->next->next!=NULL) {
slow = slow->next;
fast = fast->next->next;
}

slow->next = reverse(slow->next);
slow = slow->next;

while(slow!=NULL) {
if(dummy->val != slow->val) return false;
dummy = dummy->next;
slow = slow->next;
}
return true;
}
};```

### 234. Palindrome Linked List– Solution 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:

#Finding the Mid of the linked list
while fast and fast.next:
fast = fast.next.next
slow = slow.next

# Reversing the second half of the linked list
node = None
while slow:
n = slow.next
slow.next = node
node = slow
slow = n

# Comparing the First and the Second Half
while node: