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;
}
Problem of the day
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.
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.
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.
4 28
51 54 23 31
51 54 23 31 28
The 5th node, which is '28' has been inserted behind '31' which was the end of the list.
5 27
24 76 65 54 40
24 76 65 54 40 27
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
Time Complexity : O(N)
Space Complexity : O(1)
Assign address of the new node to an already existing node.
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 :
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).
O(1).
As we are only using the extra pointers ‘tail’ and ‘newNode’ and creating a node, the space complexity will be O(1).
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;
}
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;
}
}
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;
}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;
}
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;
}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;
}
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;
}
}