MuhammadTausif / gfg-excercises

This repo is about exercises at GFG
Apache License 2.0
0 stars 0 forks source link

Palindrome Linked List - POTD | 25-Sep-2024 #27

Closed MuhammadTausif closed 2 hours ago

MuhammadTausif commented 2 hours ago

Palindrome Linked List

Link

Difficulty: Medium

Given a singly linked list of integers. The task is to check if the given linked list is palindrome or not.

Examples:

Input: LinkedList: 1->2->1->1->2->1
Output: true
Explanation: The given linked list is 1->2->1->1->2->1 , which is a palindrome and Hence, the output is true.

Input: LinkedList: 1->2->3->4
Output: false
Explanation: The given linked list is 1->2->3->4, which is not a palindrome and Hence, the output is false.

Note: You should not use the recursive stack space as well

Constraints:

MuhammadTausif commented 2 hours ago

Solved

class Solution:
    def isPalindrome(self, head):
        a=head
        l=[]
        while a:
            l.append(a.data)
            a=a.next
        if l == l[::-1]:
            return True
        return False