简体   繁体   中英

Incorrect Linked List being returned when inserting element at first position

I am a beginner and was practicing a program on Insertion of a node at the first position when several elements are already present in the linked list. Here is my code snippet

class LinkedList{
    int data;
    LinkedList next;

    void insertNodeAtTheEnd(int d, LinkedList head){
        LinkedList temp;
        temp = head;
        while(temp.next != null){
            temp = temp.next;
        }
        LinkedList newNode = new LinkedList();
        newNode.data = d;
        newNode.next = null;
        temp.next = newNode;
    }

    void insertNodeAtGivenPosition(int d , int pos , LinkedList head){
        LinkedList temp;
        temp = head;
        int tempPos = 1;
        LinkedList newNode = new LinkedList();
        LinkedList before = temp;
        if(pos == 1){

            newNode.data = d;
            newNode.next = temp;
            temp = newNode;     
        }
        else { 
            while(temp != null){
                if(pos == tempPos){
                    newNode.data = d;
                    newNode.next = before.next ;
                    before.next = newNode;
                }

                before = temp ;
                temp = temp.next ;
                tempPos++;
            }   
        }   
    }

    void printLinked(LinkedList head){
        LinkedList temp = head;
        while(temp.next != null){
            System.out.print(temp.data + "->");
            temp = temp.next;
        }
        System.out.print(temp.data +"\n");
    }

}

class LinkedListMain{
    public static void main(String[] args){

        LinkedList node1 = new LinkedList();
        node1.data = 10;
        node1.next = null;

        LinkedList head;
        head = node1;

        head.insertNodeAtTheEnd(8, head);
        head.insertNodeAtTheEnd(6, head);
        head.insertNodeAtTheEnd(7, head);
        head.insertNodeAtTheEnd(11, head);
        head.insertNodeAtTheEnd(5, head);
        head.insertNodeAtTheEnd(2, head);
        head.printLinked(head);
        head.insertNodeAtGivenPosition(4 , 3 , head);  //line 1
        head.printLinked(head);
        head.insertNodeAtGivenPosition(1 , 1 , head);  //line2
        head.printLinked(head);             
    }
}

For the above code, after inserting element at third position (line 1) using method "insertNodeAtGivenPosition()", the code works fine and the output is shown below

10->8->6->7->11->5->2
10->8->4->6->7->11->5->2

But when I try to use the same logic to insert element at first position(line 2) and print the list, the first element is not inserted and the output looks like

10->8->6->7->11->5->2
10->8->4->6->7->11->5->2
10->8->4->6->7->11->5->2

What am I doing wrong? There seem to be some issue with the value of "head".

In order to insert at the beginning, you have to change head to reference the new node. However, head is passed to your method, which cannot change it.

In order for this to work, you'll need to return the new head :

LinkedList insertNodeAtGivenPosition(int d , int pos , LinkedList head){
    LinkedList temp;
    temp = head;
    int tempPos = 1;
    LinkedList newNode = new LinkedList();
    LinkedList before = temp;
    if(pos == 1) {
        newNode.data = d;
        newNode.next = head;
        head = newNode;     
    } else { 
        while(temp != null){
            if(pos == tempPos){
                newNode.data = d;
                newNode.next = before.next;
                before.next = newNode;
            }

            before = temp;
            temp = temp.next;
            tempPos++;
        }   
    }   
    return head;
}

And the method call should be:

head = head.insertNodeAtGivenPosition(1 , 1 , head);

Of course, you don't need the head argument, since it's an instance method, so you can change it to:

LinkedList insertNodeAtGivenPosition(int d , int pos) {
    LinkedList head = this;
    LinkedList temp = head;
    int tempPos = 1;
    LinkedList newNode = new LinkedList();
    LinkedList before = temp;
    if(pos == 1){
        newNode.data = d;
        newNode.next = head;
        head = newNode;     
    } else { 
        while(temp != null) {
            if(pos == tempPos){
                newNode.data = d;
                newNode.next = before.next ;
                before.next = newNode;
            }

            before = temp ;
            temp = temp.next ;
            tempPos++;
        }   
    }   
    return head;
}

and call it with:

head = head.insertNodeAtGivenPosition(1 , 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM