
- 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
Python Program to Reverse only First N Elements of a Linked List
When it is required to reverse a specific set of elements in a linked list, a method named ‘reverse_list’ is defined. This iterates through the list, and reverses the specific set of elements.
Below is a demonstration for the same −
Example
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList_structure: def __init__(self): self.head = None self.last_node = None def add_vals(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 print_it(self): curr = self.head while curr: print(curr.data) curr = curr.next def reverse_list(my_list, n): if n == 0: return before_val = None curr = my_list.head if curr is None: return after_val = curr.next for i in range(n): curr.next = before_val before_val = curr curr = after_val if after_val is None: break after_val = after_val.next my_list.head.next = curr my_list.head = before_val my_instance = LinkedList_structure() my_list = input('Enter the elements of the linked list... ').split() for elem in my_list: my_instance.add_vals(int(elem)) n = int(input('Enter the number of elements you wish to reverse in the list... ')) reverse_list(my_instance, n) print('The new list is : ') my_instance.print_it()
Output
Enter the elements of the linked list... 45 67 89 12 345 Enter the number of elements you wish to reverse in the list... 3 The new list is : 89 67 45 12 345
Explanation
The ‘Node’ class is created.
Another ‘LinkedList_structure’ class with required attributes is created.
It has an ‘init’ function that is used to initialize the first element, i.e the ‘head’ to ‘None’.
A method named ‘add_vals’ is defined, that helps add a value to the stack.
Another method named ‘print_it’ is defined, that helps display the values of the linked list on the console.
Another method named ‘reverse_list’ is defined, that helps reverse the specific set of elements of the linked list.
An instance of the ‘LinkedList_structure’ is created.
Elements are added to the linked list.
The elements are displayed on the console.
The number of elements that need to be reversed is taken from user.
The ‘reverse_list’ method is called on this linked list.
The output is displayed on the console.
- Related Articles
- Program to reverse a linked list in Python
- Program to reverse inner nodes of a linked list in python
- Python program to remove duplicate elements from a Doubly Linked List\n
- Python Program To Add Elements To A Linked List
- Python program to create a doubly linked list of n nodes and display it in reverse order
- Python program to create a Circular Linked List of n nodes and display it in reverse order
- Golang Program to reverse a given linked list.
- Golang program to reverse a circular linked list
- Reverse Linked List in Python
- C Program for Reverse a linked list
- Program to reverse linked list by groups of size k in Python
- Python Program to Display the Nodes of a Linked List in Reverse using Recursion
- Reverse Linked List II in C++\n
- Golang program to add elements at first and last position of linked list
- Python Program to Display the Nodes of a Linked List in Reverse without using Recursion
