1.

How To Reverse A Singly Linked List?

Answer»
  • First, set a pointer (*current) to POINT to the first node i.e. current=head.
  • Move AHEAD until current!=null (till the end)
  • set another pointer (*next) to point to the next node i.e. next=current->next
  • store reference of *next in a temporary variable (*result) i.e. current->next=result
  • swap the result value with current i.e. result=current
  • And now swap the current value with next. i.e. current=next
  • return result and repeat from step 2
  • A linked list can ALSO be reversed using RECURSION which ELIMINATES the use of a temporary variable.



Discussion

No Comment Found