Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Insert at End

Easy
0/40
profile
Contributed by
10 upvotes

Problem statement

There exists a linked list of size 'n' with nodes containing integer values. You are given an integer, which you have to insert at the end of the list.


Note : You have to return the head of the linked list after insertion.


EXAMPLE:
Input :
‘k’ = 5
list = [1, 2, 3, 4]
Output: [1, 2, 3, 4, 5]

The 5th node, which is '5' has been inserted behind '4' which was the end of the list.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line contains the integers ‘n’, and ‘k’, denoting the size of the list and the integer value to be added respectively.

The second line of each test case contains an array 'a' of positive integers denoting the nodes of the linked list.
Output format :
Print the doubly linked list after the insertion operation.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Sample Input 1:
4 28
51 54 23 31 
Sample Output 1:
51 54 23 31 28 
Explanation Of Sample Input 1 :
The 5th node, which is '28' has been inserted behind '31' which was the end of the list.
Sample Input 2 :
5 27
24 76 65 54 40 
Sample Output 2 :
24 76 65 54 40 27 
Constraints :
1 <= 'n' <= 10^4
1 <= 'a[i]',k <= 10^9 where a[i] are the values in list and k is the value being added.

Time Limit: 1 sec
Expected Complexity :
Time Complexity : O(N)
Space Complexity : O(1)
Hint

Assign address of the new node to an already existing node.

Approaches (1)
Naive Approach

Approach:
This idea is to add a new node to the end of a linked list by managing references and updating the last node.

 

Algorithm :  
 

  • Initialize a new node.
  • Save the current head in a temporary variable.
  • Set the new node's "next" pointer to NULL, indicating it's the last node.
  • If the list is empty, make the new node the head.
  • If not, traverse to the end.
  • Update the last node's "next" to point to the new node.
Time Complexity

O(N) where N is the size of the linked list.
As we are iterating the list once to reach the end, the time complexity will be O(N).

Space Complexity

O(1).
As we are only using the extra pointers ‘tail’ and ‘newNode’ and creating a node, the space complexity will be O(1).

Code Solution
(100% EXP penalty)
Insert at End
All tags
Sort by
Search icon

Interview problems

c++ solution by me

Node* insertEnd(Node* head, int k) {

    

    Node* temp=new Node(k);

    Node* ptr=head;

    if(head==NULL){

        return temp;

    }

    if(head->next==NULL){

        head->next=temp;

        return head;

    }

    while(ptr->next!=NULL){

        ptr=ptr->next;

    }

    ptr->next=temp;

    return head;

 

}

32 views
0 replies
0 upvotes

Interview problems

Java || easy solution

public class Solution {

    public static Node insertEnd(Node head, int k) {

        // Write Your Code Here.

        Node temp = head;

        if(temp == null){

            return new Node(k);

        }

 

        while(temp.next!=null){

            temp = temp.next;

        }

        temp.next = new Node(k);

 

        return head;

    }

}

105 views
0 replies
0 upvotes

Interview problems

Easiest Solution || Linked List

Node* insertEnd(Node* head, int k) {
    Node* add=new Node(k);
    if(!head)return add;
    Node* temp=head;
    while(temp->next) temp=temp->next;
    temp->next=add;
    return head;
}
144 views
0 replies
2 upvotes

Interview problems

Java Optimal Solution

public static Node insertEnd(Node head, int k) {

        if (head == null)

            return new Node(k);

 

        Node temp = head;

        while (temp.next != null) {

            temp = temp.next;

        }

 

        Node newNode = new Node(k);

        temp.next = newNode;

 

        return head;

    }

186 views
0 replies
2 upvotes

Interview problems

easy :- XD

Node* insertEnd(Node* head, int k) {
    // Write your code here.
    if(!head)
    {
        Node* temp = new Node(k);
        return temp;
    }
    Node* curr = head;
    while(curr->next != NULL)
    {
        curr = curr->next;
    }
    Node* newnode = new Node(k);
    curr->next = newnode;
    curr = newnode;

    return head;
}
61 views
0 replies
0 upvotes

Interview problems

Insert Value At Tail in Singly Linked List

Node *insertEnd(Node *head, int val) {

  // Write your code here.

  Node *block = new Node(val);

  if (head == NULL) {

    head = block;

    return head;

  }

  Node *ptr = head;

  while (ptr->next != NULL) {

    ptr = ptr->next;

  }

  ptr->next = block;

  block->next = NULL;

  return head;

}

131 views
0 replies
0 upvotes

Interview problems

Java solutions

public class Solution {

    public static Node insertEnd(Node head, int k) {

        // Write Your Code Here.

        Node newNode=new Node(k);

        if (head==null) {

            head=newNode;

        }

        else{

           Node temp=head;  

           while(temp.next!=null){

                temp=temp.next;

            }

            temp.next=newNode; 

        } 

        return head;

    }

}

164 views
0 replies
0 upvotes
Full screen
Console