hardNeetCode 150
Reverse Nodes in k-Group
Reverse nodes in k-group Example: [1,2,3,4,5], k = 2 => [2,1,4,3,5]
I reverse k nodes at a time and leave a short tail alone. The hard part is stitching each group to its neighbors.
Examples
You write
reverse_k_group(head: Optional[ListNode], k: int)
Example 1
- Input
- head = [1,2,3,4,5], k = 2
- returns
- [2,1,4,3,5]
Example 2
- Input
- head = [1,2,3,4,5], k = 3
- returns
- [3,2,1,4,5]
The pattern behind it
Linked Lists
I reach for it when
- Nodes point to the next node, and you cannot index into the list.
- You must reverse, merge, reorder or remove a node.
Say this before you type. Say which pointers you hold and which link you are about to change.
All 12 Linked Lists problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.
1Reverse Nodes in k-Grouphard
Based onLeetCode (opens in a new tab)
Prep guides that list this problem
Our company tags come from public interview write-ups. None of these is a confirmed question.