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
๐ค Output