Grokking the Coding Interview: Patterns for Coding Questions
Vote
0% completed
Solution: Problem Challenge 1: Reverse alternating K-element Sub-list
Problem Statement
Given the head of a LinkedList and a number ‘k’, reverse every alternating ‘k’ sized sub-list starting from the head.
If, in the end, you are left with a sub-list with less than ‘k’ elements, reverse it too.
Why this is an In-place Reversal of a Linked List problem
| What the question says | The signal it matches |
|---|---|
| "Given the head of a LinkedList and a number" k | the input is a linked list |
| "reverse every alternating 'k' sized sub-list starting from the head" | the wording says reverse, and only part of the list changes |
.....
.....
.....
Like the course? Get enrolled and start learning!
Shaho Shahbazpanahi
· a year ago
For this input [1,2,3,4,5,6,7,8,9,10,11] and k= 3, then expected output should be [3,2,1,6, 5, 4,10, 8,9,11, 10] while the solution give us [3,2,1,6, 5, 4,10, 8,9, 10, 11] , the problem stats if the rest of lis is smaller than k, revese it to .
L
Lucas
· 4 years ago
Here is my solution! Hope it doesnt crack under pressure :)

Reading Progress
0%