Question

Given the head of a singly linked list, reverse the list, and return the reversed list.

This is alinked_list question.

Idea

  • Uses a next, prev, and head pointer
  • Establish prev = None to be the tail of the linked list
  • While the head pointer is not None (while there are nodes in the linked list to iterate through)
  • Set next to the next node in the linked list to preserve the list order
  • Set head.next to prev to append the lower nodes of the linked list to “the next node after head”
  • Set head to next to put head in the next position in the linked list
  • Maybe just memorize this one

Solution