Reversing a Linked list
Reversing a Linked List Reversing a linked list is one of the most fundamental problems in data structures. It helps build a strong understanding of pointer manipulation and is frequently asked in ...

Source: DEV Community
Reversing a Linked List Reversing a linked list is one of the most fundamental problems in data structures. It helps build a strong understanding of pointer manipulation and is frequently asked in interviews. In this article, we will break down how to reverse a singly linked list using an iterative approach. Understanding the Problem Given a linked list like this: 1 -> 2 -> 3 -> 4 -> 5 We want to reverse it so that it becomes: 5 -> 4 -> 3 -> 2 -> 1 Structure of a Node class Node: def __init__(self, newData): self.data = newData self.next = None def reverseList(head): if head is None or head.next is None: return head # reverse the rest of linked list and put the # first element at the end rest = reverseList(head.next) # make the current head as last node of # remaining linked list head.next.next = head # update next of current head to NULL head.next = None return rest def printList(node): while node is not None: print(f"{node.data}", end="") if node.next is not N