Duno
Duno

Reputation: 47

How to insert into a Linked List at the end

I am learning linked list here https://practice.geeksforgeeks.org/problems/linked-list-insertion/1#

https://www.geeksforgeeks.org/linked-list-set-2-inserting-a-node/?ref=lbp

Here I have to write 2 functions to add a node at the beginning/the end of a linked list.

LinkedList: 9->0->5->1->6->1->2->0->5->0

The output should be: 5 2 9 5 6

But my output is: 5 2 9

I think I am wrong in the function add a node at the end, but I could not find my mistake. Could you please let me know where I am wrong?

Create a link list of size N according to the given input literals. Each integer input is accompanied by an indicator which can either be 0 or 1. If it is 0, insert the integer in the beginning of the link list. If it is 1, insert the integer at the end of the link list. Hint: When inserting at the end, make sure that you handle NULL explicitly.

Example 1:

Input:
LinkedList: 9->0->5->1->6->1->2->0->5->0
Output: 5 2 9 5 6
Explanation:
Length of Link List = N = 5
9 0 indicated that 9 should be
inserted in the beginning. Modified
Link List = 9.
5 1 indicated that 5 should be
inserted in the end. Modified Link
List = 9,5.
6 1 indicated that 6 should be
inserted in the end. Modified Link
List = 9,5,6.
2 0 indicated that 2 should be
inserted in the beginning. Modified
Link List = 2,9,5,6.
5 0 indicated that 5 should be
inserted in the beginning. Modified
Link List = 5,2,9,5,6. 
Final linked list = 5, 2, 9, 5, 6.

Here is my solution:

class Solution
{
    //Function to insert a node at the beginning of the linked list.
    Node insertAtBeginning(Node head, int x)
    {
        // code here
        Node current = new Node(x);
        current.next = head;
        head = current;
        return head;
    }
    //Function to insert a node at the end of the linked list.
    Node insertAtEnd(Node head, int x)
    {
        // code here
        Node current = head;
        while(current.next != null){
            current = current.next;
        }
        Node addthis = new Node(x);
        current = addthis;
        return head;
    }
}

Upvotes: 0

Views: 1746

Answers (2)

Dhiraj Kumar Yadav
Dhiraj Kumar Yadav

Reputation: 1

class Solution {
    //Function to insert a node at the beginning of the linked list.
    Node insertAtBeginning(Node head, int x) {
        Node node = new Node(x);
        if (head == null) {
            head = node;
            return head;
        }
        node.next = head;
        head = node;
        return head;
    }
    
    //Function to insert a node at the end of the linked list.
    Node insertAtEnd(Node head, int x) {
        Node node = new Node(x);
        if (head == null) {
            head = node;
            return head;
        }
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = node;
        return head;
    }
}

Upvotes: 0

maloomeister
maloomeister

Reputation: 2486

Indeed there is an issue in your insertAtEnd() method. You correctly iterate through to the tail of your linked list, but your problem is here:

current = addthis;

This will simply assign your new node to current, which is not what you want.

What you want is, to attach your new node to the next field of your last node like this:

current.next = addthis;

This way, you actually added your node to the previously last element of the list.

Full code:

Node insertAtEnd(Node head, int x) {
    Node current = head;
    while(current.next != null){
        current = current.next;
    }
    Node addthis = new Node(x);
    current.next = addthis;
    return head;
}

Upvotes: 1

Related Questions