Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Single Link List- Move existing object(node) to the rear end

Tags:

java

I am finishing up a small project with Linked list. I have ONE more method to complete, then I will be done. I have completed all of the following methods.

  • add front
  • add rear
  • remove front
  • remove rear
  • remove atIndex
  • remove before node
  • remove after node
  • moveFront

The last one is moveRear method

public void moveRear(int index) {
    if (index < 1 || index > size || index == 1 || index == size) {
        System.out.println("Conditions are not met");
        return;
    }
    if (head == null) {
        System.out.println("List is empty");
        return;
    }
    Node cNode = null;
    Node pNode = null;
    cNode = head;
    for (int i = 1; i < index - 1; i++) {
        cNode = cNode.next;
    }
    pNode = head;
    while (pNode.next != null) {
        pNode = pNode.next;
    }
    cNode = cNode.next.next;
    pNode = cNode.next;
    tail = cNode.next;
}

I need to link the previous node to the node after the one I am trying to move. So lets say I want to move Node 3 to the rear end. This means that I need Node 2 to link to Node 4 to keep the list connected. Then I need the LAST node to point to Node 3 and then finally have TAIL point to it.

like image 535
SkyZ Avatar asked Dec 05 '25 09:12

SkyZ


1 Answers

Also, with what you have now, you're updating the references but not changing the "next" fields of the nodes. So all you're doing is just resetting what some variables point to. You're not actually changing what cNode's and pNode's next fields, just what the variables, called cNode and pNode, point to. You should instead be updating cNode.next and pNode.next.

Now think about the characteristics of the tail node.

  1. The tail's "next" field is always null

Also, think about two conditions that are out of the ordinary for this method

  1. You want to move the front to the rear (index == 1)
  2. You want to move the rear to the rear (cNode.next == tail)

In the first scenario, you not only need to do what you would normally do but you also need to reset the front variable. In the second scenario you don't do anything because the rear is already at the rear. Assuming cNode represents the node before the one you're trying to move to the rear and pNode represents the former rear, this is what everything will look like,

public void moveRear(int index) {
    if (index < 1 || index > size || index == size) {//You actually have already 
                                                     //dealt with special case #2
        System.out.println("Conditions are not met");
        return;
    }
    if (head == null) {
        System.out.println("List is empty");
        return;
    }
    Node cNode = null;
    Node pNode = null;
    cNode = head;
    for (int i = 1; i < index-1; i++) {
        cNode = cNode.next;
    }
    pNode = head;
    while (pNode.next !=null){
        pNode = pNode.next;
    }
    if(index==1){ //special case #1
        head = cNode.next; //reset the front to the node after the front node
        pNode.next = cNode; //update the former rear's next field
        tail = pNode.next; //update the tail VARIABLE
    }
    else{ //what you would do if it was not either special case
        pNode.next = cNode.next;//reset the former rear's next field first
        cNode.next = cNode.next.next;//then change cNode's next field, ORDER MATTERS HERE
        tail = pNode.next;//then reset the rear VARIABLE

}
like image 193
Chris Gong Avatar answered Dec 06 '25 21:12

Chris Gong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!