{"id":5754,"date":"2021-10-11T08:00:03","date_gmt":"2021-10-11T08:00:03","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=5754"},"modified":"2023-07-27T13:02:37","modified_gmt":"2023-07-27T13:02:37","slug":"insert-a-node-at-a-specific-position-in-a-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/","title":{"rendered":"Insert a Node at a Specific Position in a Linked List"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012003212-Article_196.png\" alt=\"\" \/><\/p>\n<p>Linked lists are essential data structures used in computer programming to efficiently store and manage collections of elements. Inserting a new node before a specified position in a singly linked list is a common operation that allows programmers to dynamically expand and modify the list as needed.<\/p>\n<p>In this article, we will delve into the process of inserting a node before a specified position in a singly linked list using C programming language. We will explore the fundamentals of singly linked lists, understanding the node structure and the connections that form the list. By providing step-by-step explanations and practical examples, readers will gain a comprehensive understanding of how to implement the insert operation effectively. Here we will learn in detail how to insert a node at a specific position in a linked list in C.<\/p>\n<h2>How to Insert a Node at a Specific Position in a Linked List in C?<\/h2>\n<p>The problem is quite simple. We just have to insert a node at a specific position in a <a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/a-brief-introduction-to-linked-lists\/\" title=\"linked list\">linked list<\/a> in C. Here is an example to understand what is meant by to insert a node at a specific position in a linked list.<\/p>\n<p><strong>Example :<\/strong><\/p>\n<p><strong>Input:<\/strong><\/p>\n<pre><code>list = 1\u21922\u21923\u21924\u21925, \n          data = 6\n          position = 2<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code>1 \u2192 6 \u2192 2\u2192 3 \u2192 4 \u2192 5<\/code><\/pre>\n<p><strong>Explanation:<\/strong><br \/>\nSuppose the given list is 1 \u2192 2 \u2192 3 \u2192 4 \u2192 5, position = 2, and the data to be inserted is 6.<\/p>\n<ul>\n<li>According to the problem statement, we need to insert a node with value = 6 at 2nd position in the linked list.<\/li>\n<li>So, after adding the node with value = 6 at the 2nd position in the given linked list, our resultant list will be 1 \u2192 6 \u2192 2\u2192 3 \u2192 4 \u2192 5.<\/li>\n<\/ul>\n<p>Now the problem statement is clear. So, now the main question is how we should approach this problem. What is the first thing that comes to mind when we talk about insertion at a specific position in a linked list?<\/p>\n<ul>\n<li>List traversal, right? Yes. We are going to use list traversal for insertion at a specific position in a linked list.<\/li>\n<\/ul>\n<p>Before moving further to the approach section, try to think of the approach by yourself.<\/p>\n<ul>\n<li>If stuck, no problem, we will thoroughly see how we can approach this problem in the next section.<\/li>\n<\/ul>\n<p>Let\u2019s move to the approach section.<\/p>\n<h3>Approach to Insert a Node at a Specific Position in a Linked List in C<\/h3>\n<p>Our approach to insert at any position in linked list is going to be pretty simple. Here is our approach.<\/p>\n<ul>\n<li>We will simply traverse till (<strong>position -1<\/strong>)th node and add the <strong>newnode<\/strong> just after that <strong>node.<\/strong> Well, how will this work? Let us take an example.\n<ul>\n<li>Suppose the list is 1 \u2192 2 \u2192 3, and we have to insert 4 at the 2nd position.<\/li>\n<li>Now, we will traverse <strong>(position -1) = (2-1) = 1<\/strong> node and after traversing 1 node, we will be standing at 1.<\/li>\n<li>Now we will make <strong>4 \u2192 next = 1 \u2192 next<\/strong> as we have to insert it after 1, and finally, <strong>1 \u2192 next = 4<\/strong> to link the nodes.<\/li>\n<li>By doing the above steps, 4 will be added at that specific position, and our resultant linked list will be: 1 \u2192 4 \u2192 2 \u2192 3.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>We can get a clearer look at the approach by looking at the dry run.<\/p>\n<h3>Algorithm to Insert a Node at a Specific Position in a Linked List in C<\/h3>\n<p>The algorithm to insert a node at a specific position in a linked list is given below.<\/p>\n<ul>\n<li>If the position where we are asked to insert <strong>pos<\/strong> is smaller than 1 or greater than the <strong>size of the list<\/strong>, it is invalid, and we will return.<\/li>\n<li>Else, we will make a variable <strong>curr<\/strong> and make it point to the <strong>head<\/strong> of the list.<\/li>\n<li>Now we will run a for loop using <strong>curr<\/strong> to reach to the node at (<strong>pos-1<\/strong>)th position:\n<ul>\n<li>The for loop will be: <strong>for(int i=1;inext;}<\/strong><\/li>\n<li>After the termination of the above loop, <strong>curr<\/strong> will be standing at the (<strong>position \u2013 1<\/strong>)th node.<\/li>\n<li>As explained above, we will simply make <strong>newnode \u2192 next = curr \u2192 next<\/strong> and <strong>curr \u2192 next = newnode.<\/strong><\/li>\n<\/ul>\n<\/li>\n<li>If the <strong>pos<\/strong> was equal to 1, we will make the <strong>head<\/strong> point to <strong>newnode<\/strong> as <strong>newnode<\/strong> will become the first node of the list.<\/li>\n<\/ul>\n<h3>Dry Run to Insert a Node at a Specific Position in a Linked List in C<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/10\/p_1_2.png\" alt=\"\" \/><\/p>\n<p>Now, it is the showdown. Let&#8217;s see the code implementation to insert a node from linked list at a given position in C, C++, Java and in Python.<\/p>\n<h3>Code to Insert a Node at a Specific Position in a Linked List in C<\/h3>\n<p>Here is the code implementation for how to insert a node at a specific position in a linked list in C, C++, Java, and Python.<br \/>\nn.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_5755 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_5755 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_5755 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_5755 .wpsm_nav-tabs > li.active > a, #tab_container_5755 .wpsm_nav-tabs > li.active > a:hover, #tab_container_5755 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_5755 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_5755 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_5755 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_5755 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_5755 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_5755 .wpsm_nav-tabs > li > a:hover , #tab_container_5755 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_5755 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_5755 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_5755 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_5755 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_5755 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_5755 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_5755 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5755 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_5755 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5755 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_5755 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_5755\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_5755\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_5755_1\" aria-controls=\"tabs_desc_5755_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_5755_2\" aria-controls=\"tabs_desc_5755_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_5755_3\" aria-controls=\"tabs_desc_5755_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_5755_4\" aria-controls=\"tabs_desc_5755_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_5755\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_5755_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\nint size = 0;\r\n\/\/ Using this function we will be creating new nodes\r\nstruct Node* getNode(int data)\r\n{\r\n   struct Node* newNode\r\n= (struct Node*)malloc(\r\nsizeof(struct Node));\r\n    newNode-&gt;data = data;\r\n    newNode-&gt;next = NULL;\r\n    return newNode;\r\n}\r\n\/\/ Using this function we will insert the newnode at the specific position\r\nvoid insertAtPosition(struct Node* head, int pos, int data)\r\n{\r\n    if (pos &lt; 1 || pos &gt; size + 1)\r\n        printf( &quot;Invalid position!&#92;n&quot; );\r\n    else {\r\n       struct Node *curr=head;\r\n        for(int i=1;i&lt;pos-1;i++) curr=&quot;curr-&quot;&gt;next;\r\n        struct Node* temp=getNode(data);\r\n        temp-&gt;next=curr-&gt;next;\r\n        curr-&gt;next=temp;\r\n        if(pos=1)\r\n        head=temp;\r\n        size++;\r\n    }\r\n}\r\n\/\/ Using this function we will print the linked list\r\nvoid printList(struct Node* head)\r\n{\r\n    while (head != NULL) {\r\n        printf(&quot;%d &quot;,head-&gt;data);\r\n        head = head-&gt;next;\r\n    }\r\n    printf(&quot;&#92;n&quot;);\r\n}\r\n\/\/ Driver function\r\nint main()\r\n{\r\n    struct Node* head = NULL;\r\n    head = getNode(1);\r\n    head-&gt;next = getNode(2);\r\n    head-&gt;next-&gt;next = getNode(3);\r\n    head-&gt;next-&gt;next-&gt;next = getNode(4);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = getNode(5);\r\n    size = 5;\r\n    printf(&quot;Linked list before insertion: &quot;);\r\n    printList(head);\r\n    int data = 6, pos = 2;\r\n    insertAtPosition(head, pos, data);\r\n    printf(&quot;Linked list after insertion of 6 at position 2: &quot;);\r\n    printList(head);\r\n    \r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_5755_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits stdc++.h=&quot;&quot;&gt;\r\nusing namespace std;\r\n\r\n\/\/ Node structure of a singly linked list node\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n\r\nint size = 0;\r\n\r\n\/\/ Using this function we will be creating new nodes\r\nNode* getNode(int data)\r\n{\r\n    Node* newNode = new Node();\r\n\r\n    newNode-&gt;data = data;\r\n    newNode-&gt;next = NULL;\r\n    return newNode;\r\n}\r\n\r\n\/\/ Using this function we will insert the newnode at the specific position\r\nvoid insertAtPosition(Node* head, int pos, int data)\r\n{\r\n    if (pos &lt; 1 || pos &gt; size + 1)\r\n        cout &lt;&lt; &quot;Invalid position!&quot; &lt;&lt; endl;\r\n    else {\r\n        Node *curr=head;\r\n        for(int i=1;i&lt;pos-1;i++) {=&quot;&quot; curr=&quot;curr-&quot;&gt;next;\r\n        }\r\n        Node* temp=getNode(data);\r\n        temp-&gt;next=curr-&gt;next;\r\n        curr-&gt;next=temp;\r\n        if(pos=1)\r\n        head=temp;\r\n        size++;\r\n    }\r\n}\r\n\r\n\/\/ Using this function we will print the linked list\r\nvoid printList(struct Node* head)\r\n{\r\n    while (head != NULL) {\r\n        cout &lt;&lt; &quot; &quot; &lt;&lt; head-&gt;data;\r\n        head = head-&gt;next;\r\n    }\r\n    cout &lt;&lt; endl;\r\n}\r\n\r\n\/\/ Driver function\r\nint main()\r\n{\r\n\r\n    Node* head = NULL;\r\n    head = getNode(1);\r\n    head-&gt;next = getNode(2);\r\n    head-&gt;next-&gt;next = getNode(3);\r\n    head-&gt;next-&gt;next-&gt;next = getNode(4);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = getNode(5);\r\n\r\n    size = 5;\r\n\r\n    cout &lt;&lt; &quot;Linked list before insertion: &quot;;\r\n    printList(head);\r\n\r\n    int data = 6, pos = 2;\r\n    insertAtPosition(head, pos, data);\r\n    cout &lt;&lt; &quot;Linked list after insertion of 6 at position 2: &quot;;\r\n    printList(head);\r\n    \r\n    return 0;\r\n}\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_5755_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\npublic class PrepBytes {\r\n    \/\/ Structure of a singly linked list node\r\n    static class Node {\r\n        public int data;\r\n        public Node nextNode;\r\n\r\n        public Node(int data) {\r\n            this.data = data;\r\n\r\n        }\r\n    }\r\n\r\n    \/\/ Function to create a new node and return\r\n    static Node GetNode(int data) {\r\n        return new Node(data);\r\n    }\r\n\r\n    \/\/ Function to insert an element at a specified index\r\n    static Node InsertAtPos(Node headNode, int position, int data) {\r\n        Node head = headNode;\r\n        if (position &lt; 1)\r\n            System.out.print(&quot;Invalid position&quot;);\r\n\r\n        if (position == 1) {\r\n            Node newNode = new Node(data);\r\n            newNode.nextNode = headNode;\r\n            head = newNode;\r\n        } else {\r\n            for(int i=1;i&lt;position - 1;i++)\r\n            {\r\n                headNode=headNode.nextNode;\r\n            }\r\n            \r\n            Node newNode= new Node(data);\r\n            newNode.nextNode=headNode.nextNode;\r\n            headNode.nextNode=newNode;\r\n            \r\n        }\r\n        return head;\r\n    }\r\n\r\n    \/\/ Function to print the list\r\n    static void PrintList(Node node) {\r\n        while (node != null) {\r\n            System.out.print(node.data);\r\n            node = node.nextNode;\r\n            if (node != null)\r\n                System.out.print(&quot;,&quot;);\r\n        }\r\n        System.out.println();\r\n    }\r\n\r\n    \/\/ Driver function\r\n    public static void main(String[] args) {\r\n        Node head = GetNode(1);\r\n        head.nextNode = GetNode(2);\r\n        head.nextNode.nextNode = GetNode(3);\r\n        head.nextNode.nextNode.nextNode = GetNode(4);\r\n        head.nextNode.nextNode.nextNode.nextNode = GetNode(5);\r\n\r\n        System.out.print(&quot;Linked list before insertion: &quot;);\r\n        PrintList(head);\r\n\r\n        int data = 6, pos = 2;\r\n        head = InsertAtPos(head, pos, data);\r\n        System.out.print(&quot;Linked list after&quot; + &quot; insertion of 6 at position 2: &quot;);\r\n        PrintList(head);\r\n    }\r\n}\r\n\r\n\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_5755_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"Python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n# A linked list Node\r\nclass Node:\r\n    def __init__(self, data):\r\n        self.data = data\r\n        self.nextNode = None\r\n\r\n# function to create and return a Node\r\ndef getNode(data):\r\n\r\n    newNode = Node(data)\r\n    return newNode\r\n\r\n# function to insert a Node at required position\r\ndef insertPos(headNode, position, data):\r\n    head = headNode\r\n    \r\n    if (position &lt; 1):    \r\n        print(&quot;Invalid position!&quot;)\r\n    \r\n    if position == 1:\r\n        newNode = Node(data)\r\n        newNode.nextNode = headNode\r\n        head = newNode\r\n            \r\n    else:\r\n    \r\n        while (position != 0):        \r\n            position -= 1\r\n\r\n            if (position == 1):\r\n\r\n                newNode = getNode(data)\r\n                newNode.nextNode = headNode.nextNode\r\n                headNode.nextNode = newNode\r\n                break\r\n            \r\n            headNode = headNode.nextNode\r\n            if headNode == None:\r\n                break        \r\n        if position != 1:\r\n            print(&quot;position out of range&quot;)    \r\n    return head\r\n    \r\ndef printList(head):\r\n    while (head != None):    \r\n        print(' ' + str(head.data), end = '')\r\n        head = head.nextNode\r\n    print()\r\n    \r\nif __name__=='__main__':\r\n    \r\n    head = getNode(1)\r\n    head.nextNode = getNode(2)\r\n    head.nextNode.nextNode = getNode(3)\r\n    head.nextNode.nextNode.nextNode = getNode(4)\r\n    head.nextNode.nextNode.nextNode.nextNode = getNode(5)\r\n    print(&quot;Linked list before insertion: &quot;, end='')\r\n    printList(head)\r\n    data = 6\r\n    position = 2\r\n    head = insertPos(head, position, data)\r\n    print(&quot;Linked list after insertion of 6 at position 2: &quot;, end = '')\r\n    printList(head)\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_5755 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_5755 a\"),jQuery(\"#tab-content_5755\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output:<\/strong><\/p>\n<pre><code>Linked list before insertion: 1,2,3,4,5\nLinked list after insertion of 6 at position 2: 1,6,2,3,4,5<\/code><\/pre>\n<p><strong>Time Complexity:<\/strong> O(n) is the time complexity to insert a node before specified position in singly linked list in C programming, as we are traversing the whole linked list to reach the desired position.<\/p>\n<p><strong>Space Complexity:<\/strong> O(1) is the space complexity to insert a node before specified position in singly linked list in C programming, since we have not used any extra space for inserting a single node in the linked list.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nIn conclusion, inserting a node before a specified position in a singly linked list using C offers a powerful way to dynamically expand and modify the list&#8217;s content. Throughout this article, we explored the intricacies of singly linked lists, understanding the node structure and the connections that form the list. By providing step-by-step explanations and practical examples, readers gained a comprehensive understanding of how to efficiently implement the insert operation.<\/p>\n<p>We learned that the insert operation involves traversing the linked list to find the target position, creating a new node, and updating pointers to seamlessly integrate the new node into the list. We also discussed essential memory management considerations to prevent memory leaks and ensure efficient memory allocation during the insert operation.<\/p>\n<h2>Frequently Asked Questions (FAQs) on insert a node before specified position in singly linked list in C<\/h2>\n<p>Here are some Frequently Asked Questions on how to insert a node at a specific position in a linked list in C.<br \/>\n<strong>1. Can I insert a node at the beginning (head) of the singly linked list?<\/strong><br \/>\nYes, to insert a node at the head, simply update the head pointer to point to the new node, and set the new node&#8217;s next pointer to the current head.<\/p>\n<p><strong>2. What happens if the specified position is beyond the list&#8217;s length?<\/strong><br \/>\nIf the specified position is beyond the list&#8217;s length, the new node will be inserted at the end of the list.<\/p>\n<p><strong>3. How does the time complexity of the insert operation in a singly linked list compare to an array?<\/strong><br \/>\nThe time complexity of the insert operation in a singly linked list is O(n), where n is the position of insertion. In an array, it is O(n) as well, but it may require shifting elements after the insertion point, resulting in a potential O(n) for worst-case scenarios.<\/p>\n<p><strong>4. Can I insert multiple nodes before the same specified position?<\/strong><br \/>\nYes, you can insert multiple nodes before the same position by repeating the insert operation with different data.<\/p>\n<p><strong>5. What is the role of a temporary pointer in the insert operation?<\/strong><br \/>\nThe temporary pointer is used to traverse the linked list to find the specified position for the new node&#8217;s insertion.<\/p>\n<p><strong>6. Is it possible to insert a node before the first occurrence of a specific value in the linked list?<\/strong><br \/>\nYes, you can find the first occurrence of the specific value while traversing the list and then perform the insert operation before that position.<\/p>\n<p><strong>7. Can I insert a node before a specified position in a doubly linked list using similar steps?<\/strong><br \/>\nYes, the process of inserting a node before a specified position in a doubly linked list is similar, but it requires updating both the previous and next pointers of the nodes involved.<\/p>\n<table width=\"641\">\n<tbody>\n<tr>\n<td colspan=\"2\" width=\"641\" style=\"text-align: center\"><strong>Related Articles<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/doubly-circular-linked-list-introduction-and-insertion\/\">Doubly circular linked list in data structure<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/advantages-disadvantages-and-uses-of-a-doubly-linked-list\/\">Application of doubly linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/applications-of-linked-list-data-structure\/\">Applications of linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/difference-between-a-singly-linked-list-and-a-doubly-linked-list\/\">Singly linked list vs doubly linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/advantage-and-disadvantage-of-linked-list-over-array\/\">Advantages and disadvantages of linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/menu-driven-program-for-all-operations-on-doubly-linked-list-in-c\/\">Doubly linked list all operations in C<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/binary-search\/binary-search-on-linked-list\/\">Binary search in linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/bubble-sort-for-linked-list-by-swapping-nodes\/\">Bubble sort linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/delete-a-node-in-doubly-linked-list\/\">Deletion in doubly linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/delete-middle-of-linked-list\/\">Delete the middle node of a linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/adding-two-polynomials-using-linked-list\/\">Polynomial addition using linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/find-the-smallest-and-largest-elements-in-a-singly-linked-list\/\">Find max value and min value in linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list-articles\/student-record-management-system-using-linked-list\/\">Student management system using linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/swap-nodes-in-a-linked-list-without-swapping-data\/\">Swap nodes in linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/add-two-numbers-represented-by-linked-lists-set-1\/\">Add two numbers represented by linked lists<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/find-the-first-node-of-the-loop-in-a-linked-list\/\">Find starting point of loop in linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/merge-sort-on-a-singly-linked-list\/\">Merge sort linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/delete-a-node-at-a-given-position\/\">Delete a node from linked list at a given position<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/remove-duplicates-from-an-unsorted-linked-list\/\">Remove duplicates from unsorted linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/python\/python-program-to-reverse-a-linked-list\/\">Reverse a linked list in Python<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Linked lists are essential data structures used in computer programming to efficiently store and manage collections of elements. Inserting a new node before a specified position in a singly linked list is a common operation that allows programmers to dynamically expand and modify the list as needed. In this article, we will delve into the [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[125],"tags":[],"class_list":["post-5754","post","type-post","status-publish","format-standard","hentry","category-linked-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Insert a Node at a Specific Position in a Linked List<\/title>\n<meta name=\"description\" content=\"Explaining how to insert node at specific position in linked list using an efficient approach. We understood the algorithm and the implementation of code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Insert a Node at a Specific Position in a Linked List\" \/>\n<meta property=\"og:description\" content=\"Explaining how to insert node at specific position in linked list using an efficient approach. We understood the algorithm and the implementation of code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-10-11T08:00:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-27T13:02:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012003212-Article_196.png\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Insert a Node at a Specific Position in a Linked List\",\"datePublished\":\"2021-10-11T08:00:03+00:00\",\"dateModified\":\"2023-07-27T13:02:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/\"},\"wordCount\":1421,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012003212-Article_196.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/\",\"name\":\"Insert a Node at a Specific Position in a Linked List\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012003212-Article_196.png\",\"datePublished\":\"2021-10-11T08:00:03+00:00\",\"dateModified\":\"2023-07-27T13:02:37+00:00\",\"description\":\"Explaining how to insert node at specific position in linked list using an efficient approach. We understood the algorithm and the implementation of code.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012003212-Article_196.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012003212-Article_196.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linked list articles\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/linked-list\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Insert a Node at a Specific Position in a Linked List\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Insert a Node at a Specific Position in a Linked List","description":"Explaining how to insert node at specific position in linked list using an efficient approach. We understood the algorithm and the implementation of code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Insert a Node at a Specific Position in a Linked List","og_description":"Explaining how to insert node at specific position in linked list using an efficient approach. We understood the algorithm and the implementation of code.","og_url":"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-10-11T08:00:03+00:00","article_modified_time":"2023-07-27T13:02:37+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012003212-Article_196.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Insert a Node at a Specific Position in a Linked List","datePublished":"2021-10-11T08:00:03+00:00","dateModified":"2023-07-27T13:02:37+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/"},"wordCount":1421,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012003212-Article_196.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/","name":"Insert a Node at a Specific Position in a Linked List","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012003212-Article_196.png","datePublished":"2021-10-11T08:00:03+00:00","dateModified":"2023-07-27T13:02:37+00:00","description":"Explaining how to insert node at specific position in linked list using an efficient approach. We understood the algorithm and the implementation of code.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012003212-Article_196.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645012003212-Article_196.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/insert-a-node-at-a-specific-position-in-a-linked-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Linked list articles","item":"https:\/\/prepbytes.com\/blog\/category\/linked-list\/"},{"@type":"ListItem","position":3,"name":"Insert a Node at a Specific Position in a Linked List"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=5754"}],"version-history":[{"count":17,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5754\/revisions"}],"predecessor-version":[{"id":17396,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5754\/revisions\/17396"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=5754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=5754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=5754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}