Reverse a Linked List in groups of a given size

Data Structures and Algorithms Note

Allie Hsu
Coder Life
2 min readJan 21, 2022

--

how I would do

For the purpose of reverse linked list, think about using stack first!

curr will stop at the first node of the next round until it reached the last node.next, which is None.

the element stored in the stack will be popped and linked as a set, when the stack is empty, continue the process for the next round.

when the second last.next = new_stack.pop(), which means the new_stack become empty, and prev are assign as the last Node.

when curr is None, stop the while loop and at the end, make the next of prev, which is the last Node point to None to end the linked list.

--

--

Allie Hsu
Coder Life

A tech enthusiast who is keen to develop new skills