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.- class Node:
- def __init__(self, data):
- self.data = data
- self.next = None
- class LinkedList:
- def __init__(self):
- self.head = None
- self.last_node = None
- def append(self, data):
- if self.last_node is None:
- self.head = Node(data)
- self.last_node = self.head
- else:
- self.last_node.next = Node(data)
- self.last_node = self.last_node.next
- def get_prev_node(self, ref_node):
- current = self.head
- while current and current.next != ref_node:
- current = current.next
- return current
- def is_palindrome(llist):
- if llist.head is None:
- return True
- start = llist.head
- end = llist.last_node
- while start != end and end.next != start:
- if start.data != end.data:
- return False
- start = start.next
- end = llist.get_prev_node(end)
- return True
- # ---------- MAIN PROGRAM ----------
- while True:
- print("\n=============== Check if Singly Linked List is Palindrome ===============\n")
- a_llist = LinkedList()
- data_list = input(
- "Please enter the elements in the linked list: ").split()
- for data in data_list:
- a_llist.append(int(data))
- if is_palindrome(a_llist):
- print("The linked list is palindromic.")
- else:
- print("The linked list is not palindromic.")
- opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
- if opt == "no":
- print("Exiting program...")
- break
- elif opt != "yes":
- print("Invalid choice. Exiting program...")
- 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