Advertisement

Reverse Linked List - LeetCode 206 Solution

Reverse Linked List - Complete Solution Guide

Reverse Linked List is LeetCode problem 206, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the head of a singly linked list, reverse the list, and return the reversed list . Example 1: Input: head = [1,2,3,4,5] Output: [5,4,3,2,1] Example 2: Input: head = [1,2] Output: [2,1] Example 3: Input: head = [] Output: [] Constraints: The number of nodes in the list is the range [0, 5000] . -5000 <= Node.val <= 5000 Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?

Detailed Explanation

The problem asks you to reverse a singly linked list. A singly linked list is a data structure where each node points to the next node in the sequence. The input is the head (first node) of the linked list, and the output should be the head of the reversed linked list. For example, if the input list is 1->2->3->4->5, the output should be 5->4->3->2->1. The list can be empty or contain only one node.

Solution Approach

The provided code uses an iterative approach to reverse the linked list. It iterates through the list, reversing the links between nodes one by one. This is done by keeping track of the previous node (`prev`), the current node (`curr`), and the next node (`next`). In each iteration, the `next` pointer of the current node is redirected to point to the previous node. Then, `prev` and `curr` are updated to move to the next node in the original list.

Step-by-Step Algorithm

  1. Step 1: Initialize three pointers: `prev` to `NULL` (or `nullptr`), `curr` to the `head` of the list, and `next` to `NULL` (or `nullptr`).
  2. Step 2: Iterate while `curr` is not `NULL` (or `nullptr`).
  3. Step 3: Store the `next` node in the `next` pointer: `next = curr->next`.
  4. Step 4: Reverse the link: `curr->next = prev`.
  5. Step 5: Update `prev` and `curr`: `prev = curr`, `curr = next`.
  6. Step 6: After the loop, `prev` will point to the new head of the reversed list. Return `prev`.

Key Insights

  • Insight 1: The core idea is to iterate through the linked list, changing the `next` pointer of each node to point to the previous node. This requires careful manipulation of pointers.
  • Insight 2: Three pointers are crucial: `prev`, `curr`, and `next`. `prev` tracks the previously visited node, `curr` is the currently processed node, and `next` stores a temporary reference to the next node to avoid losing it.
  • Insight 3: Handling the base cases (empty list or single-node list) is important. The algorithm should gracefully handle these scenarios without errors.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Linked List, Recursion.

Companies

Asked at: Accenture, ByteDance, Cisco, Deloitte, Expedia, Google, J.P. Morgan, Luxoft, NetApp, Nvidia, Oracle, Ozon, PayPal, Paytm, Qualcomm, SAP, Samsung, ServiceNow, Siemens, Snap, Tesla, Tinkoff, Visa, X, Yandex, Yelp, Zenefits, Zopsmart, Zynga, tcs.