Taking on a new challenge: solving GeeksforGeeks POTD daily and sharing my solutions! π»π₯
The goal: sharpen problem-solving skills, level up coding, and learn something new every day. Follow my journey! π
100DaysOfCode #CodingChallenge #ProblemSolving #GeeksforGeeks #DeveloperJourney
Problem:
https://www.geeksforgeeks.org/problems/check-if-linked-list-is-pallindrome/1
Palindrome Linked List
Difficulty: Medium Accuracy: 41.48%
You are given the head of a singly linked list of positive integers. You have to check if the given linked list is palindrome or not.
Examples:
Input:

Output: true
Explanation: The given linked list is 1 -> 2 -> 1 -> 1 -> 2 -> 1, which is a palindrome.
Input:

Output: false
Explanation: The given linked list is 10 -> 20 -> 30 -> 40 -> 50, which is not a palindrome.
Constraints:
1 β€ number of nodes β€ 105
0 β€ node->data β€ 103
Solution:
class Solution:
def isPalindrome(self, head):
if not head or not head.next:
return True
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
prev = None
curr = slow
while curr:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
p1 = head
p2 = prev
while p2:
if p1.data != p2.data:
return False
p1 = p1.next
p2 = p2.next
return True
Top comments (0)