Reverse LinkedList {Loop implementation}

In the first part I’ve described how to reverse single LinkedList using recursion. Today we will to do the same using one loop.
The structure of linked list will be the same, look to the Node class.
OK, What we should understand? The first implementation used next steps:

  • Go throughout all nodes and set next to null { after these steps we have n-null pointed LinkedLists.}
  • Read from end and change direction of linking.

Current implementation will have different approach and will be implemented using next steps:

  • Create reversed root node.
  • Read next node.
  • Save as next reversed root.
  • Set node to reversed root.
  • Set next node to node; {For making loop}.

Steps are good but example better 🙂, So, we have next list:

1->2->3->4->null

First algorithm {recursion implementation} works using two-time loop {recursion} from start to end and from end to start , it’s like recursion working.

So, After first time loop {recursion from start to end} we will have n-lists:

1->null; 2->null; 3->null; 4->null;

During second time loop {recursion from end to start } we add

n-1->n-2

elements and will have reversed list

4->3->2->1->null

and on the start of second loop {recursion from end to start } we have first element of reversed list and just add new elements to end .

So, what is different to current implementation ?

We have the same list:

1->2->3->4->null

and we have just a one loop. So, the main different is changing root element of reversed list. In other words new element will be added to head or better to new element will be added tail of reversed list.

Explanation of above steps:

We have root of reversed list

reversedList = null

read first element and add tail {reversedList} as next to first element after it assign first element as tail to {reversedList} for next loop iteration, so we will have

reversedList = 1->null

after second iteration will have

reversedList = 2->1->null

and so on.

Code:

package com.alychidesigns.list.reverse;

import com.alychidesigns.list.Node;

/**
 *@author
 */
public class LoopReverser implements Reverser {
    @Override
    public Node reverse(Node node) {
        Node prev = null;
        while (node!=null){
            Node next = node.getNext();
            node.setNext(prev);
            prev = node;
            node = next;
        }
        return prev;
    }
}

Pros:

  • Faster than first implementation because use one loop from start to end
  • Do not use Stack Memory

Cons:

  • Hard to understand for some people {because use diff approach than recursion and stack}

Read More Post

STAY CONNECTED

Most Popular

Why Serviced Apartments Are the Perfect Choice for Long-Term Stays in Singapore – Everything You Need to Know
17 Dec

Why Serviced Apartments Are the Perfect Choice for Long-Term Stays in Singapore – Everything You Need to Know

If you’re heading to Singapore for a few weeks or many months, maybe for work or study or just to

Manifesting Your Dream Career: Finding Your Passion And Purpose
01 Aug

Manifesting Your Dream Career: Finding Your Passion And Purpose

Are you passionate about finding your dream career? Manifesting can help you align your career with your passions and purpose.

Revolutionizing Your Ride: Expert Auto Service At Carolina Auto Service
27 Feb

Revolutionizing Your Ride: Expert Auto Service At Carolina Auto Service

Finding a reliable auto service provider is paramount in the bustling world of vehicle maintenance and repair. That's where Carolina

Floratam: The Robust Grass Solution From Council Growers SOD
25 Jan

Floratam: The Robust Grass Solution From Council Growers SOD

Floratam grass, a popular St. Augustine variety, has become a go-to choice for homeowners and landscapers seeking a lush, durable

Carolina Mobile Autoservice: Your Go-To For Expert Brake Service Repair
25 Jan

Carolina Mobile Autoservice: Your Go-To For Expert Brake Service Repair

For vehicle owners, the braking system is among the most critical components for safety and performance. Carolina Mobile Autoservice, a

Safeguarding Your Property: Expert Water Damage Services By MBC Capital
24 Jan

Safeguarding Your Property: Expert Water Damage Services By MBC Capital

Water damage can significantly impact homes and businesses, often leaving a trail of destruction and disruption in its wake. Understanding