How to Find the Nth Node from the End of a Linked List in Python

In this tutorial, we will learn how to program "How to Find the Nth Node from the End of a Linked List in Python." The objective is to find the nth node from the end of a linked list. This tutorial will guide you step by step through the process of finding the nth node. 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 Find the Nth Node from the End of a Linked List . 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.  
  21. def length_llist(llist):
  22.     length = 0
  23.     current = llist.head
  24.     while current:
  25.         length += 1
  26.         current = current.next
  27.     return length
  28.  
  29.  
  30. def return_n_from_last(llist, n):
  31.     l = length_llist(llist)
  32.     current = llist.head
  33.     for _ in range(l - n):
  34.         current = current.next
  35.     return current.data
  36.  
  37.  
  38. while True:
  39.     print("\n============= Find the Nth Node from the End of a Linked List =============\n")
  40.  
  41.     a_llist = LinkedList()
  42.  
  43.     data_list = input(
  44.         "Please enter the elements in the linked list (space-separated): "
  45.     ).split()
  46.  
  47.     for data in data_list:
  48.         a_llist.append(int(data))
  49.  
  50.     n = int(input("Enter n (nth element from the end): "))
  51.     value = return_n_from_last(a_llist, n)
  52.  
  53.     print(f"The {n}th element from the end is: {value}")
  54.  
  55.     # Ask user if they want to try again
  56.     opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
  57.     if opt == "no":
  58.         print("Exiting program...")
  59.         break
  60.     elif opt != "yes":
  61.         print("Invalid choice. Exiting program...")
  62.         break

This program finds the nth node from the end of a singly linked list. It allows the user to create a linked list by entering elements, then calculates the total length of the list by traversing it once. Using this length, the program determines the position of the desired node from the beginning and retrieves its value accordingly. The result is displayed clearly to the user, and the program includes an option to repeat the process or exit, making it interactive and user-friendly.

Output:

There you have it we successfully created How to Find the Nth Node from the End of a Linked List 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