The algorithm: Initialize prev = null, curr = head While curr is not null: - Save next = curr.next (don't lose the rest of the list) - Reverse the pointer: curr.next = prev - Move prev forward: prev = curr - Move curr forward: curr = next Return prev (the new head) After the loop, prev points to the last node (now the new head).
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
$ curl repovive.com/roadmaps/maang-interview-prep/linked-lists/reverse-linked-list-solution
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░███████████████████████████████████████████████████████████████████████████████████████████