
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to check linked list items are forming palindrome or not 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 [5,4,3,4,5], then this is a palindrome, but a list like [5,4,3,2,1] is not a palindrome.
To solve this, we will follow these steps −
- fast := head, slow := head, rev := None and flag := 1
- if 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 value of fast is not same as value of slow, then return false
- fast := next of fast, and slow := next of slow
- return true
Let us see the following implementation to get better understanding −
Example
class ListNode: def __init__(self, data, next = None): self.data = data self.next = next def make_list(elements): head = ListNode(elements[0]) for element in elements[1:]: ptr = head while ptr.next: ptr = ptr.next ptr.next = ListNode(element) return head class Solution(object): def isPalindrome(self, head): fast,slow = head,head rev = None flag = 1 if not head: 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 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 head = make_list([5,4,3,4,5]) ob1 = Solution() print(ob1.isPalindrome(head))
Input
[5,4,3,4,5]
Output
True
- Related Articles
- Program to check points are forming convex hull or not in Python
- Program to check points are forming concave polygon or not in Python
- Program to check whether domain and range are forming function or not in Python
- Python Program to Check whether a Singly Linked List is a Palindrome
- Program to check heap is forming max heap or not in Python
- Check if a doubly linked list of characters is palindrome or not in C++
- Program to check a string is palindrome or not in Python
- Program to check two parts of a string are palindrome or not in Python
- Palindrome Linked List in Python
- Python program to check if a string is palindrome or not
- Program to check string is palindrome or not with equivalent pairs in Python
- Program to check string is palindrome with lowercase characters or not in Python
- Check if all elements of the array are palindrome or not in Python
- Program to check whether inorder sequence of a tree is palindrome or not in Python
- Python Program to Check Whether a String is a Palindrome or not Using Recursion

Advertisements