VOOZH about

URL: https://www.javacodegeeks.com/2015/11/coding-reversing-unordered-single-linked-list-using-2-pointers.html

⇱ Coding: Reversing Unordered Single Linked List using 2 Pointers - Java Code Geeks


Puzzle

πŸ‘ coding
Given an Unordered Single Linked List, provide an Algorithm to reverse such Linked List using only 2 pointers.

Input

A Single Linked List.

Example. 1 -> 4 -> 3 -> 2 -> 0

Output

A Reversed Single Linked List.

Example. 0 -> 5 -> 3 -> 2 -> 1

Solution Using Java as Programming Language

Complete Code Base is spread on 3 Gist snippets, as follows

  • Abstract Class defining the Unsorted Linked List ADT and implementing banal methods,
  • Implementation Class defining the business logic for the Unsorted Linked List,
  • Test Class defining the suite of test cases for the Unsorted Linked List ADT implementation.

Hereafter, an extract of the code to show up how to realize the assignment using an iterative approach.

public void iterativeReverse()
{
 if(head == null || head.next == null)
 return;

 Node p1 = head, p2 = p1.next, tmp = null;
 p1.next = null;
 while(p1 != p2) { // reverse links incrementally
 if(p2.next == null) {
 head = p2;
 tmp = null;
 } else
 tmp = p2.next;
 p2.next = p1;
 p1 = p2;
 if(tmp != null)
 p2 = tmp;
 }
}

How does the Algorithm evolve? Well, looking at the following diagram everything should be clear.

πŸ‘ everse Unsorted Linked List
everse Unsorted Linked List

As intuitive, the same problem can be solved using recursion; hereafter an extract of the code that shows up how to approach the solution using recursion.

public void recursiveReverse()
{
 if(head == null || head.next == null)
 return;

 Node p1 = head, p2 = p1.next;
 p1.next = null;
 _reverse(p1, p2); // reverse recursively
}

private void _reverse(Node p1, Node p2)
{
 if(p2.next == null) {
 head = p2;
 p2.next = p1;
 p1 = p2;
 } else {
 Node tmp = p2.next;
 p2.next = p1;
 p1 = p2;
 p2 = tmp;
 _reverse(p1, p2);
 }
}

Discussion

The main idea consists in reversing the pointers incrementally. As clear from the proposed diagram, two additional pointers aid in scanning through the overall Unsorted Single Linked List, and one pointer at time the inversion operation is accomplished. From the code, some corner cases have to be taken into consideration; for instance, the first operation on pointer one (i.e p1 in the picture, when it points to the head of the list) has to nullify the pointer to next for obvious reasons: p2 will point back to p1, so p1 pointing to p2 would create a loop. On the other hand, p2 has to be moved until the temporary placeholder is different from null: this will create the situation in which at the end of the processing p1 and p2 will point to the same tail element. Such condition (i.e. p1 equals p2, that in turn is equal to the tail of the list) represents the termination check.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Paolo Maresca
Paolo Maresca
November 23rd, 2015Last Updated: November 22nd, 2015
3 172 2 minutes read

Paolo Maresca

Paolo is a Sr Software Engineer with a diversified experience in the ICT Industry. He is a passionate and committed Distributed Systems Engineer that daily tries to put the bar higher. He is polyglot: he masters Java SE/EE, C/C++, Python, JavaScript, Bash and he is getting proficient with Scala. He is PC member of international conferences like IEEE and IARIA. He blogs. He is an independent thinker!
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Oldest
Newest Most Voted
jabbarish
10 years ago

package listreverse; public class List { private static class Node { public Node(V item, Node next) { value = item; this.next = next; } Node next; V value; } Node head; Node last; public void add(T item) { Node x = new Node(item, null); if (last == null) { head = x; } else { last.next = x; } last = x; } void print() { System.out.println(β€œβ€”β€œ); Node ptr = head; while (ptr != null) { System.out.println(ptr.value); ptr = ptr.next; } } void reverse() { last = head; head = null; Node next; while (true) { next = last.next; last.next… Read more Β»

0
Reply
James
10 years ago

I think that’s supposed to be a four not a five…

0
Reply
10 years ago
Reply to  James

Hi James,
you’re right, that’s a typo that I fixed on the blog. The output (as in the picture) is β€˜0 -> 2 -> 3 -> 4 -> 1’.
Thanks for pointing this out!
Cheers,
P

0
Reply
Back to top button
Close
wpDiscuz