How to reverse singly link list?
Submitted by: AdministratorReverse a singly link list using iteration:-
1. First, set a pointer ( *current) to point to the first node i.e. current=base
2. Move ahead until current!=null (till the end)
3. set another pointer (* next) to point to the next node i.e. next=current->next
4. store reference of *next in a temporary variable ( *result) i.e current->next=result
5. swap the result value with current i.e. result=current
6. and now swap the current value with next. i.e. current=next
7. return result and repeat from step 2
A linked list can also be reversed using recursion which eliminates the use of a temporary variable.
Submitted by: Administrator
1. First, set a pointer ( *current) to point to the first node i.e. current=base
2. Move ahead until current!=null (till the end)
3. set another pointer (* next) to point to the next node i.e. next=current->next
4. store reference of *next in a temporary variable ( *result) i.e current->next=result
5. swap the result value with current i.e. result=current
6. and now swap the current value with next. i.e. current=next
7. return result and repeat from step 2
A linked list can also be reversed using recursion which eliminates the use of a temporary variable.
Submitted by: Administrator
Read Online Data Structure Linked list Job Interview Questions And Answers
Top Data Structure Linked list Questions
☺ | Suppose In an integer array, there is 1 to 100 number, out of one is duplicate, how to find? |
☺ | What is circular linked list? |
☺ | Explain linked list using C++ with an example? |
☺ | Explain the steps to insert data into a singly linked list? |
☺ | Explain how to find 3rd element from end in a linked list in one pass? |
Top Data Structure Categories
☺ | Data Center Manager Interview Questions. |
☺ | Creative UI/UX Designers Interview Questions. |
☺ | Sort And Searching Interview Questions. |
☺ | Data Structure Linked list Interview Questions. |
☺ | Data Structure Arrays Interview Questions. |