1"use strict";(self.webpackChunkpatternize_io=self.webpackChunkpatternize_io||[]).push([[745],{78478:(e,n,t)=>{t.d(n,{A:()=>s});var i=t(96540),a=t(92303);function s(e){let n=e.children,t=e.fallback;return(0,a.A)()?i.createElement(i.Fragment,null,null==n?void 0:n()):null!=t?t:null}},6633:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>c,contentTitle:()=>u,default:()=>L,frontMatter:()=>d,metadata:()=>p,toc:()=>m});var i=t(58168),a=t(98587),s=(t(96540),t(15680)),r=t(52340),o=t(78478);const l=["components"],d={id:"Insertion",title:"Linked List Insertion",sidebar_label:"Insertion Operations",description:"Learn how to insert elements into a linked list with detailed explanations of head insertion, tail insertion, and position-based insertion. Includes time complexity analysis and code examples.",keywords:["linked list insertion","linked list operations","data structures","insert at head","insert at tail","insert at position","time complexity","java implementation"]},u="Linked List Insertion",p={unversionedId:"DataStructures/Basic/LinkedList/Insertion",id:"DataStructures/Basic/LinkedList/Insertion",title:"Linked List Insertion",description:"Learn how to insert elements into a linked list with detailed explanations of head insertion, tail insertion, and position-based insertion. Includes time complexity analysis and code examples.",source:"@site/docs/DataStructures/Basic/LinkedList/Insertion.md",sourceDirName:"DataStructures/Basic/LinkedList",slug:"/DataStructures/Basic/LinkedList/Insertion",permalink:"/DataStructures/Basic/LinkedList/Insertion",draft:!1,tags:[],version:"current",frontMatter:{id:"Insertion",title:"Linked List Insertion",
1sidebar_label:"Insertion Operations",description:"Learn how to insert elements into a linked list with detailed explanations of head insertion, tail insertion, and position-based insertion. Includes time complexity analysis and code examples.",keywords:["linked list insertion","linked list operations","data structures","insert at head","insert at tail","insert at position","time complexity","java implementation"]},sidebar:"CS Core",previous:{title:"Deletion Operations",permalink:"/DataStructures/Basic/LinkedList/Deletion"},next:{title:"Linked List Introduction",permalink:"/DataStructures/Basic/LinkedList/linked-list-data-structure"}},c={},m=[{value:"Visualization",id:"visualization",level:2},{value:"Types of Insertion",id:"types-of-insertion",level:2},{value:"1. Insert at Head",id:"1-insert-at-head",level:3},{value:"2. Insert at Tail",id:"2-insert-at-tail",level:3},{value:"3. Insert at Position",id:"3-insert-at-position",level:3},{value:"Common Insertion Patterns",id:"common-insertion-patterns",level:2}],y={toc:m},g="wrapper";function L(e){let n=e.components,t=(0,a.A)(e,l);return(0,s.yg)(g,(0,i.A)({},y,t,{components:n,mdxType:"MDXLayout"}),(0,s.yg)("h1",{id:"linked-list-insertion"},"Linked List Insertion"),(0,s.yg)("h2",{id:"visualization"},"Visualization"),(0,s.yg)("p",null,"Watch how elements are inserted into a linked list:"),(0,s.yg)(o.A,{mdxType:"BrowserOnly"},(()=>(0,s.yg)(r.$D,{mdxType:"LLInsertOperation"}))),(0,s.yg)("h2",{id:"types-of-insertion"},"Types of Insertion"),(0,s.yg)("h3",{id:"1-insert-at-head"},"1. Insert at Head"),(0,s.yg)("ul",null,(0,s.yg)("li",{parentName:"ul"},"Time Complexity: O(1)"),(0,s.yg)("li",{parentName:"ul"},"Most efficient insertion operation")),(0,s.yg)("pre",null,(0,s.yg)("code",{parentName:"pre",className:"language-java"},"public void insertAtHead(int val) {\n ListNode newNode = new ListNode(val);\n newNode.next = head;\n head = newNode;\n}\n")),(0,s.yg)("h3",{id:"2-insert-at-tail"},"2. Insert at Tail"),(0,s.yg)("ul",null,(0,s.yg)("li",{parentName:"ul"},"Time Complexity: O(n)"),(0,s.yg)("li",{parentName:"ul"},"Requires traversal to end")),(0,s.yg)("pre",null,(0,s.yg)("code",{parentName:"pre",className:"language-java"},"public void insertAtTail(int val) {\n ListNode newNode = new ListNode(val);\n if (head == null) {\n head = newNode;\n return;\n }\n \n ListNode current = head;\n while (current.next != null) {\n current = current.next;\n }\n current.next = newNode;\n}\n")),(0,s.yg)("h3",{id:"3-insert-at-position"},"3. Insert at Position"),(0,s.yg)("ul",null,(0,s.yg)("li",{parentName:"ul"},"Time Complexity: O(n)"),(0,s.yg)("li",{parentName:"ul"},"Requires traversal to position")),(0,s.yg)("pre",null,(0,s.yg)("code",{parentName:"pre",className:"language-java"},"public void insertAtPosition(int val, int position) {\n if (position == 0) {\n insertAtHead(val);\n return;\n }\n \n ListNode newNode = new ListNode(val);\n ListNode current = head;\n for (int i = 0; i < position - 1 && current != null; i++) {\n current = current.next;\n }\n \n if (current == null) return;\n \n newNode.next = current.next;\n current.next = newNode;\n}\n")),(0,s.yg)("h2",{id:"common-insertion-patterns"},"Common Insertion Patterns"),(0,s.yg)("ol",null,(0,s.yg)("li",{parentName:"ol"},"Using dummy head node"),(0,s.yg)("li",{parentName:"ol"},"Maintaining tail pointer"),(0,s.yg)("li",{parentName:"ol"},"Two-pointer technique for position finding"),(0,s.yg)("li",{parentName:"ol"},"Recursive insertion")))}L.isMDXComponent=!0}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.