1.

Describe how you would reverse a singly linked list.

Answer»

The TASK is to reverse a linked list given a pointer to the HEAD node. By modifying the linkages between nodes, we can reverse the list.

Iterative method:

  • Set up three-pointers. prev is set to NULL, curr is set to head, and NEXT is set to NULL.
  • Go through the linked list one by one. Do the following in a loop.
// Before CHANGING next of CURRENT, // store next node next = curr->next// Now change next of current // This is where actual reversing happens curr->next = prev // Move prev and curr one step forward prev = curr curr = next


Discussion

No Comment Found