How to Check if Singly Linked List is Palindrome in Python

In this tutorial, we will learn how to program “How to Check if a Singly Linked List Is a Palindrome in Python.”*The objective is to check whether a singly linked list is a palindrome or not. This tutorial will guide you step by step through the process of identifying a palindrome in a singly linked list. By the end of this tutorial, you will have a solid understanding of how to implement this task effectively in Python, helping you strengthen your problem-solving abilities and improve your coding skills.

This topic is straightforward and easy to understand. Simply follow the instructions provided, and you will complete it with ease. The program will guide you step by step through the process of checking whether a singly linked list is a palindrome. So, let’s dive into the coding process!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. class Node:
  2.     def __init__(self, data):
  3.         self.data = data
  4.         self.next = None
  5.  
  6.  
  7. class LinkedList:
  8.     def __init__(self):
  9.         self.head = None
  10.         self.last_node = None
  11.  
  12.     def append(self, data):
  13.         if self.last_node is None:
  14.             self.head = Node(data)
  15.             self.last_node = self.head
  16.         else:
  17.             self.last_node.next = Node(data)
  18.             self.last_node = self.last_node.next
  19.  
  20.     def get_prev_node(self, ref_node):
  21.         current = self.head
  22.         while current and current.next != ref_node:
  23.             current = current.next
  24.         return current
  25.  
  26.  
  27. def is_palindrome(llist):
  28.     if llist.head is None:
  29.         return True
  30.  
  31.     start = llist.head
  32.     end = llist.last_node
  33.  
  34.     while start != end and end.next != start:
  35.         if start.data != end.data:
  36.             return False
  37.         start = start.next
  38.         end = llist.get_prev_node(end)
  39.  
  40.     return True
  41.  
  42.  
  43. # ---------- MAIN PROGRAM ----------
  44.  
  45. while True:
  46.     print("\n=============== Check if Singly Linked List is Palindrome ===============\n")
  47.  
  48.     a_llist = LinkedList()
  49.  
  50.     data_list = input(
  51.         "Please enter the elements in the linked list: ").split()
  52.  
  53.     for data in data_list:
  54.         a_llist.append(int(data))
  55.  
  56.     if is_palindrome(a_llist):
  57.         print("The linked list is palindromic.")
  58.     else:
  59.         print("The linked list is not palindromic.")
  60.  
  61.     opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
  62.     if opt == "no":
  63.         print("Exiting program...")
  64.         break
  65.     elif opt != "yes":
  66.         print("Invalid choice. Exiting program...")
  67.         break

This Python program determines whether a singly linked list is a palindrome, meaning the sequence of elements reads the same forward and backward. It constructs the linked list using `Node` and `LinkedList` classes, where user-provided elements are appended one by one. The `is_palindrome` function works by comparing values from the beginning (`start`) and the end (`end`) of the list, moving inward step by step. Since the list is singly linked, a helper method is used to locate the previous node when moving backward from the end. If any pair of elements does not match, the function immediately returns `False`; otherwise, it confirms the list is palindromic. The program runs in a loop, allowing users to test multiple linked lists until they choose to exit.

Output:

There you have it we successfully created How to Check if Singly Linked List is Palindrome in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials