EASY NC#35 Blind #40 Linked List

206. Reverse Linked List

๐Ÿ“– Problem

Given the head of a singly linked list, reverse the list, and return the reversed list.

๐Ÿง  Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • โ€ขArray methods (`push`, `pop`, `shift`, `slice`)
  • โ€ขObject/Map/Set usage patterns
  • โ€ขFunction parameter and return typing

Logical Thinking Concepts

  • โ€ขDefine invariants before coding
  • โ€ขCheck edge cases first (`[]`, single element, duplicates)
  • โ€ขEstimate time/space before implementation
  • โ€ขApply Recursion reasoning pattern

๐Ÿ’ก Approach

  • โ†’ Use three pointers: prev, current, next
  • โ†’ Save next pointer before changing current.next
  • โ†’ Point current.next to prev
  • โ†’ Move prev and current forward
  • โ†’ Time: O(n), Space: O(1)

๐Ÿ› ๏ธ Hints & Pitfalls

Hints

  • โ€ขUse three pointers: prev, current, next
  • โ€ขSave next pointer before changing current.next
  • โ€ขPoint current.next to prev

Common Pitfalls

  • โ€ขMove prev and current forward
  • โ€ขTime: O(n), Space: O(1)

๐Ÿงช Test Cases

Hidden tests on submit: 3

Test Case 1
Not run
Input:
createList([1, 2, 3, 4, 5]);
Expected:
{"val":1,"next":{"val":2,"next":{"val":3,"next":{"val":4,"next":{"val":5,"next":null}}}}}
Test Case 2
Not run
Input:
createList([1]);
Expected:
{"val":1,"next":null}
Test Case 3
Not run
Input:
createList([]);
Expected:
null

๐Ÿ“ Code Editor

๐Ÿ“š Reference Solution

โ–ผ
โŒ˜K Search โŒ˜โ†ฉ Run โŒ˜S Submit