Saturday, September 30, 2023
HomeSoftware EngineeringThe right way to Swap Node Pairs In Linked Listing in Java

The right way to Swap Node Pairs In Linked Listing in Java


The problem

If you’re given the top node in a linked listing, write a way that swaps every pair of nodes within the listing, then returns the top node of the listing.

Instance:

if you’re given an inventory ordered A,B,C,D the ensuing listing must be B,A,D,C.

The listing will probably be composed of Nodes of the next specification:

public class Node {
    non-public String worth;
    public Node subsequent;

    public Node(String worth) { this.worth = worth; }

    public String getValue() { return worth; }
    
    public String toString() { return this.worth; }
    
    public String printList() {
      if (this.subsequent == null) return this.toString() + " ->";
      return this.toString() + " -> " + subsequent.printList();
    }
}

The answer in Java code

Choice 1:

public class LinkedListPairs {
  public static Node swapPairs(Node head)  head.subsequent == null) return head;
    Node a = head, b = head.subsequent, c = b.subsequent;
    b.subsequent = a; a = b; b = a.subsequent; b.subsequent = swapPairs(c);
    return a;
  
}

Choice 2:

public class LinkedListPairs {
    public static Node swapPairs(Node head) 
}

Option3:

public class LinkedListPairs {
    public static Node swapPairs(Node head) {
        Node prev = new Node("temp");
        prev.subsequent = head;

        Node consequence = prev;
        Node first = head;

        whereas (first != null && first.subsequent != null) {
            Node second = first.subsequent;

            // Swap Pair
            prev.subsequent = second;
            first.subsequent = second.subsequent;
            second.subsequent = first;

            // Replace Pointers
            prev = first;
            first = first.subsequent;
        }

        return consequence.subsequent;
    }
}

Take a look at circumstances to validate our answer

import org.junit.Take a look at;
import static org.junit.Assert.*;

public class LinkedListPairsTest {

    @Take a look at
    public void basicTests() {
        executeTest(null, LinkedListPairs.swapPairs(null));
        executeTest(new Node("A"), new Node("A"));
        executeTest(new ListBuilder().withValue("B").withValue("A").withValue("D").withValue("C").construct(), new ListBuilder().withValue("A").withValue("B").withValue("C").withValue("D").construct());
    }

    // use this to construct your personal exams
    non-public class ListBuilder {
        non-public Node head = null, final = null;

        public ListBuilder withValue(String worth) {
            if (head == null) {
                head = new Node(worth);
                final = head;
            } else {
                final.subsequent = new Node(worth);
                final = final.subsequent;
            }
            return this;
        }

        public Node construct() {
            return head;
        }
    }

    non-public static void executeTest(Node enter, Node expectedResult) {
        Node output = LinkedListPairs.swapPairs(enter);
        if (expectedResult == null) {
            assertNull(output);
            return;
        }
        
        ultimate String anticipated = expectedResult.printList();
        ultimate String precise = output.printList();
        ultimate String errMsg = "Anticipated '" + anticipated;
        assertEquals(errMsg, anticipated, precise);
    }

}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments