https://leetcode.com/problems/reverse-linked-list/

  • As the list is singly list, we must flip it around on the first pass, otherwise risk losing the next
  • We hold 2 *Node pointers: one prev to keep track of the previous node, so that we can connect back to it, and one temp to track the head->next
// Initialise temp as nullptr
while (head != nullptr) {
	temp = head->next; // save the next node
	head->next = prev; // repoint next to the previous node
	prev = head; // now the previous node is the head
	head = temp; // head is the next
}