Doubly linked list Insertion at given position | Practice | GeeksforGeeks

Given a doubly-linked list, a position p, and an integer x. The task is to add a new node with value x at the position just after pth node in the doubly linked list.
Example 1:
Input:
LinkedList: 2<->4<->5
p = 2, x = 6 
Output: 2 4 5 6
Explanation: p = 2, and x = 6. So, 6 is
inserted after p, i.e, at position 3
(0-based indexing).

Example 2:
Input:
LinkedList: 1<->2<->3<->4
p = 0, x = 44
Output: 1 44 2 3 4
Explanation: p = 0, and x = 44 . So, 44
is inserted after p, i.e, at position 1
(0-based indexing).
Your Task:
The task is to complete the function addNode() which head reference, position and data to be inserted as the arguments, with no return type.
Expected Time Complexity : O(N)
Expected Auxilliary Space : O(1)
Constraints:
1 <= N <= 104
0 <= p < N
 
0
mayank20211 week ago
C++ : 0.1/1.19void addNode(Node *head, int pos, int data){   if(pos<0) pos=0;     Node* newnode= new Node(data);  Node* temp=head;      while(pos--)        temp=temp->next;
          newnode->prev=temp;        newnode->next=temp->next;        temp->next=newnode;                if(newnode->next)        newnode->next->prev=newnode;        }
+1
chouhanakshat211 month ago
Easy Solution
 void addNode(Node head, int pos, int data)
	{
		  Node node1 = new Node(data);

        if(head==null)
        {
            node1.prev = null;
            head = node1;
            return;
        }

        Node n=head;
        int count=0;

        while (count!=pos)
        {
            n=n.next;
            count++;
        }

        if(n.next==null)
        {
            //last k next me new node ka ref.
            n.next=node1;
            // new node k previous me last ka ref.
            node1.prev=n;
            return;
        }

        //new node k next me n ka next
        node1.next=n.next;
        
        
        //n k next ka previous ka ref me node1 k ref
        n.next.prev=node1;

        //n k next me node1 ka ref.
        n.next=node1;

        //node1 k prev me ka next ref.
        node1.prev=n;
	}
+3
rmn51241 month ago

//Function to insert a new node at given position in doubly linked list.
void addNode(Node *head, int pos, int data)
{
   if(!head) return;
   
   Node*temp=head;
   while(pos--){
       temp=temp->next;
   }
   Node*node=new Node(data);
   if(!temp->next){  // if it's last node
       temp->next=node;
       node->prev=temp;
       node->next=NULL;
       return;
   }
   Node*nxt=temp->next; // else update 4 links
   temp->next=node;
   node->next=nxt;
   nxt->prev=temp;
   node->prev=temp;
   return;
}
0
hpthor202011 month ago
def addNode(head, p, data):   # Code here   temp=Node(data)   if head==None and p==1:       return temp   if head==None and pos>1:       return None   current=head   while p:       p-=1       current=current.next   if current.next==None:       temp.prev=current       current.next=temp       return head   temp.next=current.next   current.next.prev=temp   temp.prev=current   current.next=temp   return head
+1
tarunkanade2 months ago
2.2/5.2
void addNode(Node head_ref, int pos, int data)
	{
		// Your code here
		Node cur = head_ref, temp = new Node(data);
		
		for(int i=1; i <= pos; i++){
		    cur = cur.next;
		}
		
		temp.next = cur.next;
		cur.next = temp;
		temp.prev = cur;
	}
0
sadiiqyare662 months ago
void addNode(Node head, int pos, int data)
	{
		// Your code here
		Node temp = new Node(data);
		Node curr = head;
		
		    for(int i = 0; i < pos; i++){
		        curr = curr.next;
		    }
		    
		    if(curr.next != null){
		        temp.next = curr.next;
		        curr.next = temp;
		        temp.prev = curr;
		        curr.next.prev = temp;
		    }
		    else{
		        curr.next = temp;
		        temp.prev = curr;
		    }
		    
		
	}
0
priyeshanand92 months ago
void addNode(Node *head, int pos, int data){  int c=0;  pos++;  Node *p=head;  while(p){      c++;      if(c==pos) break;      p=p->next;  }  Node *q=new Node(data);  Node *temp=p->next;  p->next=q;  q->prev=p;  q->next=temp;  if(temp!=NULL)  temp->prev=q;}
 
+2
madhukartemba2 months ago
JAVA SOLUTION:
class GfG
{
    //Function to insert a new node at given position in doubly linked list.
    void addNode(Node head_ref, int pos, int data)
	{
	    int count = 0;
	    while(head_ref!=null && count!=pos)
	    {
	        count++;
	        head_ref = head_ref.next;
	    }
	    
	    Node nextnode = head_ref.next;
	    head_ref.next = new Node(data);
	    head_ref.next.prev = head_ref;
	    head_ref.next.next = nextnode;
	    if(nextnode!=null) nextnode.prev = head_ref.next;
	    
	    
	}
}
0
surabhichoubey552 months ago
void addNode(struct Node *head, int pos, int data){   if(head == NULL)   {       return;   }   if(pos == -1 && head!=NULL)   {      struct Node *newnode = new Node(data);//if the givin possision is -1 thats means      //we wants to insert node after p which is head possision      }   struct Node*temp = head;   while(temp!=NULL&& pos)   {       temp = temp->next;       pos--;   }   if(temp->next!=NULL)//if temp next is not null thats mean after temp there is a node   //which addres we will store in aftertempNode then do furthure task   {       struct Node *newnode = new Node(data);       struct Node *aftertempNode = temp->next;       temp->next = newnode;       newnode->prev = temp;       newnode->next = aftertempNode;       aftertempNode->prev = newnode;          }   else{    //we will reach here when temp next is null thats mean we are inserting    //a node at last of the linked list    struct Node *newnode = new Node(data);    temp->next = newnode;     newnode->prev = temp;   }}
+1
krishnakantbha2 months ago
Best C++ Solution
 
void addNode(Node *head, int pos, int data){   Node* temp = new Node(data);   Node* curr = head;      int cnt = 0;   while(cnt != pos){       curr = curr->next;       cnt++;   }
   temp->next = curr->next;   if(curr->next != NULL)       curr->next->prev = temp;   curr->next = temp;   temp->prev = curr;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.