Reverse Linked List.
Reverse a Singly Linked List Objective: - Given the head of a singly linked list, reverse the list so that: The first node becomes the last The last node becomes the first All next pointers are reversed The operation is done in-place Return the new head of the reversed list. Understanding the Problem A singly linked list is a sequence of nodes where each node contains: A value A reference to the next node Example: 1 → 2 → 3 → 4 → null After reversing: 4 → 3 → 2 → 1 → null Intuition Here is how the linked list looks like. We are given a “head”. Which is pointing to the first node. (Node stores the value and the address to the next node.) So, What we can do here is if we store the next node’s address to the previous one then we can reverse the Linked List. In Linked List each node points to the next node so we traverse in the List and swap the address to pointing backward then maybe we reverse the LL. Let’s Try… So we required two pointers that help us to do that. Let’s take “previous” ...