Quick Overview

This question evaluates understanding of linked list data structures and proficiency manipulating pointers or references to perform in-place transformations when reversing a linked list. Commonly asked in Coding & Algorithms interviews, it gauges algorithmic thinking and practical application-level competency in list operations rather than purely conceptual theory.

Reverse a linked list

Company: LinkedIn

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

##### Question LeetCode 206. Reverse Linked List — Given the head of a singly linked list, reverse the list and return the reversed list. https://leetcode.com/problems/reverse-linked-list/description/

Overview: This question evaluates understanding of linked list data structures and proficiency manipulating pointers or references to perform in-place transformations when reversing a linked list. Commonly asked in Coding & Algorithms interviews, it gauges algorithmic thinking and practical application-level competency in list operations rather than purely conceptual theory.

Given the head of a singly linked list, reverse the list in-place and return the new head. Each node has fields: val (int) and next (reference to the next node or None). Do not allocate new nodes or modify node values; only change next pointers. For examples and tests, the linked list is represented as an array of values; the function receives a linked list head and must return the head of the reversed list.

Constraints

  • 0 <= n <= 100000 where n is the number of nodes
  • -10^9 <= Node.val <= 10^9
  • Must run in O(n) time
  • Use O(1) extra space
  • head may be None (empty list)

Hints

  1. Maintain three pointers: prev, curr, next.
  2. Iteratively set curr.next = prev, then advance all pointers.
  3. A recursive solution is possible: reverse the rest, then set head.next.next = head and head.next = None.

Reverse Singly Linked List

mediumLinkedInSoftware EngineerLinked List

Given the head of a singly linked list, reverse the list in-place and return the new head. Each node has fields: val (int) and next (reference to the next node or None). Do not allocate new nodes or modify node values; only change next pointers. For examples and tests, the linked list is represented as an array of values; the function receives a linked list head and must return the head of the reversed list.

Examples

Example 1
Input
head = [1,2,3,4,5]
Output
[5,4,3,2,1]
Notes
Reverse pointers iteratively: 1->2->3->4->5 becomes 5->4->3->2->1.
Example 2
Input
head = [1]
Output
[1]
Notes
Single-node list remains the same.

Constraints

  • •0 <= n <= 100000 where n is the number of nodes
  • •-10^9 <= Node.val <= 10^9
  • •Must run in O(n) time
  • •Use O(1) extra space
  • •head may be None (empty list)

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def reverse_list(head: list[int]) -> list[int]:
Sample input
[1,2,3,4,5]
Language
Case 1
Input
[
  1,
  2,
  3,
  4,
  5
]
Expected
[
  5,
  4,
  3,
  2,
  1
]
Case 2
Input
[]
Expected
[]

Browse More Questions

Show the approach

Approach

Iterate through the list while keeping a pointer to the previous node. For each node, store curr.next, redirect curr.next to prev, then move prev and curr forward. This reverses all next pointers in-place and returns the last processed node (prev) as the new head.

Time complexity:
O(n)
Space complexity:
O(1)