# Palindrome Linked List in Python

Suppose we have a linked list. We have to check whether the list elements are forming a palindrome or not. So if the list element is like [1,2,3,2,1], then this is a palindrome.

To solve this, we will follow these steps −

• fast := head, slow := head, rev := None and flag := 1

• if the head is empty, then return true

• while fast and next of fast is available

• if next of the next of fast is available, then set flag := 0 and break the loop

• fast := next of the next of fast

• temp := slow, slow := next of slow

• next of temp := rev, and rev := temp

• fast := next of slow, and next of slow := rev

• if flag is set, then slow := next of slow

• while fast and slow are not None,

• if the value of fast is not the same as the value of slow, then return false

• fast := next of fast, and slow := next of slow

• return true

## Example (Python)

Let us see the following implementation to get a better understanding −

Live Demo

class ListNode:
def __init__(self, data, next = None):
self.data = data
self.next = next
def make_list(elements):
for element in elements[1:]:
while ptr.next:
ptr = ptr.next
ptr.next = ListNode(element)
class Solution(object):
rev = None
flag = 1
return True
while fast and fast.next:
if not fast.next.next:
flag = 0
break
fast = fast.next.next
temp = slow
slow = slow.next
temp.next = rev
rev = temp
#print(fast.val)
fast = slow.next
slow.next = rev
if flag:
slow = slow.next
while fast and slow:
if fast.data != slow.data:
return False
fast = fast.next
slow = slow.next
return True
print(ob1.isPalindrome(head))
[1,2,3,2,1]
True