0

I have been trying to come up with an algorithm to swap 2 nodes (not necesarily right next to each other) in a singly linked list for 2 days but for some reason I cannot do it. Here is what I have, I am really new to coding and have been really stressed: enter image description here I have managed to place a temp node in but can't actually swap the nodes.

public void swap(int i, int j) {
    current = head;
    current2 = head;
    sllNode temp = new sllNode(" ");
    sllNode temp2 = new sllNode(" ");

    for(int z = 0; i>z; z++)
        current=current.next;
    for(int q = 0; j>q; q++)
        current2 = current2.next;

    temp.next = current2.next.next;
    current.next = temp;
    current.next = current2.next.next;
    current2.next = current;
3
  • Include your code in the question please. Commented Oct 23, 2016 at 21:14
  • 2
    Please do not post your code as image! Commented Oct 23, 2016 at 21:14
  • To swap two of anything, a and b, you must do something like the following. tmp = a; a = b; b = tmp; Commented Oct 23, 2016 at 21:34

3 Answers 3

3

Why exchange nodes, when you can exchange the data?

public void swap(int i, int j) {

    sllNode ithNode = head;
    for (int z = 0; z < i; z++) {
        ithNode = ithNode.next;
    }

    sllNode jthNode = head;
    for (int q = 0; q < j; q++) {
        jthNode = jthNode.next;
    }

    // Swap the data        
    String data = ithNode.data;
    ithNode.data = jthNode.data;
    jthNode.data = data;
}

It would make sense to use a method:

public sllNode get(int i) {
    sllNode current = head;
    while (i > 0) {
        current = current.next;
    }
    return current;
}

By the way:

  • The convention for class names is a beginning capital: SllNode.
  • Do not use fields for things like current and current2 where they can be local variables.

Exchanging nodes, the hard way

Here one has to think, so it is best to deal with special cases first, and then only treat i < j.

public void swap(int i, int j) {
    if (i >= size() || j >= size()) {
        throw new IndexOutOfBoundsException();
    }
    if (i == j) {
        return;
    }
    if (j < i) {
        swap(j, i);
        return;
    }

    // i < j

    sllNode ithPredecessor = null;
    sllNode ithNode = head;
    for (int z = 0; z < i; z++) {
        ithPredecessor = ithNode;
        ithNode = ithNode.next;
    }

    sllNode jthPredecessor = ithNode;
    sllNode jthNode = ithNode.next;
    for (int q = i + 1; q < j; q++) {
        jthPredecessor = jthNode;
        jthNode = jthNode.next;
    }

    // Relink both nodes in the list:

    // - The jthNode:
    if (ithPredecessor == null) {
        head = jthNode;
    } else {
        ithPredecessor.next = jthNode;
    }
    sllNode jNext = jthNode.next;
    //if (ithNode.next == jthNode) {
    if (jthPredecessor == ithNode) {
        jthNode.next = ithNode;
    } else {
        jthNode.next = ithNode.next;
    }

    // - The ithNode:
    if (jthPredecessor == ithNode) {
    } else {
        jthPredecessor.next = ithNode;
    }
    ithNode.next = jNext;
}

No guarantee that the logic is okay. There are tricks:

    //if (ithNode.next == jthNode) {
    if (jthPredecessor == ithNode) {

Both conditions test whether i + 1 == j, but testing on a .next and then assigning makes the condition a momentary state. As you see it would have been easier to have one single if (i + 1 == j) { ... } else { ... } and handle both the ithNode and jthNode.

Sign up to request clarification or add additional context in comments.

1 Comment

Part of the reason I want to exchange the nodes is because I am trying to gain a deeper understanding of nodes. I know it may not be the most effective method but I care more about the understanding for now.
0

To do this, you need to swap 2 things: the node as next from the previous node, and the next node.

Once you found current and current2 which are the previous nodes of the nodes you want to swap, do this:

Swap the nodes:

sllNode tmp = current.next;
current.next = current2.next;
current2.next = tmp;

Then swap the next:

tmp = current.next.next;
current.next.next = current2.next.next;
current2.next.next = tmp;

Comments

0

// Swapping two elements in a Linked List using Java

import java.util.*;

class SwappingTwoElements {

public static void main(String[] args)
{

    LinkedList<Integer> ll = new LinkedList<>();

    // Adding elements to Linked List
    ll.add(10);
    ll.add(11);
    ll.add(12);
    ll.add(13);
    ll.add(14);
    ll.add(15);

    // Elements to swap
    int element1 = 11;
    int element2 = 14;

    System.out.println("Linked List Before Swapping :-");

    for (int i : ll) {
        System.out.print(i + " ");
    }

    // Swapping the elements
    swap(ll, element1, element2);
    System.out.println();
    System.out.println();

    System.out.println("Linked List After Swapping :-");

    for (int i : ll) {
        System.out.print(i + " ");
    }
}

// Swap Function
public static void swap(LinkedList<Integer> list,
                        int ele1, int ele2)
{

    // Getting the positions of the elements
    int index1 = list.indexOf(ele1);
    int index2 = list.indexOf(ele2);

    // Returning if the element is not present in the
    // LinkedList
    if (index1 == -1 || index2 == -1) {
        return;
    }

    // Swapping the elements
    list.set(index1, ele2);
    list.set(index2, ele1);
}

} Output Before Swapping Linked List :- 10 11 12 13 14 15

After Swapping Linked List :- 10 14 12 13 11 15 Time Complexity: O(N), where N is the Linked List length

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.