Assuming you have gone through LinkedList in java and know about linked list. This post contains different examples for reversing a linked list which are given below:

1. By writing our own function(Using additional space): reverseLinkedList() method contains logic for reversing string objects in a linked list. This method takes a linked list as a parameter, traverses all the elements in reverse order and adds it to the newly created linked list.

Algorithm:
Step 1. Create a linked list with n elements
Step 2. Create an empty linked list which will be used to store reversed elements
Step 3. Start traversing the list from ‘n’ to ‘0’ and store the elements in the newly created list.
Step 4. The elements will be stored in the following order: n, n-1, n-2, ……0
Step 5. Return the list to the caller and print it

Example:
Step 1: LL: 1 -> 2 -> 3 -> 4 -> 5 where 'LL' is the linked list with n elements
Step 2: 'Rev' is an empty linked list
Step 3: Start traversing, the below passes are the intermediate steps while traversing
1st pass: Rev: 5
2nd pass: Rev: 5 -> 4
3rd pass: Rev: 5 -> 4 -> 3
4th pass: Rev: 5 -> 4 -> 3 -> 2
5th pass: Rev: 5 -> 4 -> 3 -> 2 -> 1
Step 4: nth element of 'LL' is stored in 0th position of 'Rev',
n-1 element of LL is stored in 1st position of Rev and so on......
Step 5: Return Rev: 5 -> 4 -> 3 -> 2 -> 1  to the calling function.

Time Complexity: O(n)
Space Complexity: O(n)

NOTE: As we are using additional memory space for storing all the reversed ‘n’ elements, the space complexity is O(n).

Output:

Elements before reversing: [Cherry, Chennai, Bullet]
Elements after reversing: [Bullet, Chennai, Cherry]

2. By writing our own function(Without using additional space): In the previous example, a linked list is used additionally for storing all the reversed elements which takes more space. To avoid that, same linked list can be used for reversing.

Algorithm:
1. Create a linked list with n elements
1. Run the loop for n/2 times where ‘n’ is the number of elements in the linkedlist.
2. In the first pass, Swap the first and nth element
3. In the second pass, Swap the second and (n-1)th element and so on till you reach the mid of the linked list.
4. Return the linked list after loop termination.

Example:
Input: 1 -> 2 -> 3 -> 4 -> 5
1st pass: (swap first and nth element)
5 -> 2 -> 3 -> 4 -> 1
2nd pass: (swap second and (n-1)th element)
5 -> 4 -> 3 -> 2 -> 1
3rd pass: (reached mid, Terminate loop)
5 -> 4 -> 3 -> 2 -> 1
Output: 5 -> 4 -> 3 -> 2 -> 1

Time Complexity: O(n/2)
Space Complexity: O(1)

Output:

Elements before reversing: [1, 2, 3, 4, 5]
Elements after reversing: [5, 4, 3, 2, 1]

3. By using Collections class: Collections is a class in java.util package which contains various static methods for searching, sorting, reversing, finding max, min….etc. We can make use of the In-built Collections.reverse() method for reversing an linked list. It takes a list as an input parameter and returns the reversed list.

NOTE: Collections.reverse() method uses the same algorithm as “By writing our own function(Without using additional space)”

Time Complexity: O(n/2)
Space Complexity: O(1)

Output:

Elements before reversing: [1, 2, 3, 4, 5]
Elements after reversing: [5, 4, 3, 2, 1]

4.Reversing a linked list of user defined objects: An Employee class is created for creating user defined objects with employeeID, employeeName, departmentName as class variables which are initialized in the constructor. An linked list is created that takes only Employee(user defined) Objects. These objects are added to the linked list using add() method. The linked list is reversed using In-built reverse() method of Collections class.

printElements() method is used to iterate through all the user defined objects in the linked list and print the employee ID, name and department name for every object.

Time Complexity: O(n/2)
Space Complexity: O(1)

Output:

Elements before reversing:
EmpID:123, EmpName:Cherry, Department:Fashionist
EmpID:124, EmpName:muppala, Department:Development
EmpID:125, EmpName:Bullet, Department:Police
Elements after reversing:
EmpID:125, EmpName:Bullet, Department:Police
EmpID:124, EmpName:muppala, Department:Development
EmpID:123, EmpName:Cherry, Department:Fashionist

See reverse a linked list for reversing a user defined linked list.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : SujanM

Article Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.