In a singly linked list, how do you reverse the list?

Practice Questions

Q1
In a singly linked list, how do you reverse the list?
  1. Swap elements
  2. Use a stack
  3. Iterate and change pointers
  4. Use recursion

Questions & Step-by-Step Solutions

In a singly linked list, how do you reverse the list?
  • Step 1: Start with three pointers: previous (set to null), current (set to the head of the list), and next (set to null).
  • Step 2: While the current pointer is not null, do the following:
  • a. Set the next pointer to the current's next node.
  • b. Change the current's next pointer to point to the previous node.
  • c. Move the previous pointer to the current node.
  • d. Move the current pointer to the next node (which you saved earlier).
  • Step 3: Once the loop is done, set the head of the list to the previous pointer, as it will now be the new head.
No concepts available.
Soulshift Feedback ×

On a scale of 0–10, how likely are you to recommend The Soulshift Academy?

Not likely Very likely