{"id":26716,"date":"2017-12-22T20:08:01","date_gmt":"2017-12-22T14:38:01","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26716"},"modified":"2017-12-22T20:08:01","modified_gmt":"2017-12-22T14:38:01","slug":"python-programming-inserting-node-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-programming-inserting-node-linked-list\/","title":{"rendered":"Python Programming &#8211; Inserting a node in Linked List | Set 2"},"content":{"rendered":"<p>We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.<span id=\"more-142784\"><\/span><\/p>\n<p>All programs discussed in this post consider following representations of linked list<\/p>\n<p><strong>Python Programming:<\/strong><\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Node%20class%0Aclass%20Node%3A%0A%20%0A%20%20%20%20%23%20Function%20to%20initialize%20the%20node%20object%0A%20%20%20%20def%20__init__(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20self.data%20%3D%20data%20%20%23%20Assign%20data%0A%20%20%20%20%20%20%20%20self.next%20%3D%20None%20%20%23%20Initialize%20next%20as%20null%0A%20%0A%23%20Linked%20List%20class%0Aclass%20LinkedList%3A%0A%20%20%20%0A%20%20%20%20%23%20Function%20to%20initialize%20the%20Linked%20List%20object%0A%20%20%20%20def%20__init__(self)%3A%20%0A%20%20%20%20%20%20%20%20self.head%20%3D%20None\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>In this post, methods to insert a new node in linked list are discussed. A node can be added in three ways<br \/>\n<strong>1)<\/strong> At the front of the linked list<br \/>\n<strong>2) <\/strong>After a given node.<br \/>\n<strong>3)<\/strong> At the end of the linked list.<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Add a node at the front: (A 4 steps process)<\/strong><br \/>\nThe new node is always added before the head of the given Linked List. And newly added node becomes the new head of the Linked List. For example if the given Linked List is 10->15->20->25 and we add an item 5 at the front, then the Linked List becomes 5->10->15->20->25. Let us call the function that adds at the front of the list is push(). The push() must receive a pointer to the head pointer, because push must change the head pointer to point to the new node (See this)<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignleft size-full wp-image-26695\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_insert_at_start.png\" alt=\"C Algorithm - Inserting a node in Linked List | Set 2\" width=\"759\" height=\"255\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_insert_at_start.png 759w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_insert_at_start-300x101.png 300w\" sizes=\"(max-width: 759px) 100vw, 759px\" \/><\/p>\n<p>Following are the 4 steps to add node at the front.<\/p>\n<p><strong>Python Programming:<\/strong><\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20This%20function%20is%20in%20LinkedList%20class%0A%23%20Function%20to%20insert%20a%20new%20node%20at%20the%20beginning%0Adef%20push(self%2C%20new_data)%3A%0A%20%0A%20%20%20%20%23%201%20%26%202%3A%20Allocate%20the%20Node%20%26%0A%20%20%20%20%23%20%20%20%20%20%20%20%20Put%20in%20the%20data%0A%20%20%20%20new_node%20%3D%20Node(new_data)%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%23%203.%20Make%20next%20of%20new%20Node%20as%20head%0A%20%20%20%20new_node.next%20%3D%20self.head%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%23%204.%20Move%20the%20head%20to%20point%20to%20new%20Node%20%0A%20%20%20%20self.head%20%3D%20new_node\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Time complexity of push() is O(1) as it does constant amount of work.<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Add a node after a given node: (5 steps process)<\/strong><br \/>\nWe are given pointer to a node, and the new node is inserted after the given node.<\/p>\n<p><img decoding=\"async\" class=\"alignleft size-full wp-image-26698\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_insert_middle.png\" alt=\"C Programming - Inserting a node in Linked List | Set 2\" width=\"759\" height=\"273\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_insert_middle.png 759w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_insert_middle-300x108.png 300w\" sizes=\"(max-width: 759px) 100vw, 759px\" \/><br \/>\n<strong>Python Programming:<\/strong><\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20This%20function%20is%20in%20LinkedList%20class.%0A%23%20Inserts%20a%20new%20node%20after%20the%20given%20prev_node.%20This%20method%20is%20%0A%23%20defined%20inside%20LinkedList%20class%20shown%20above%20*%2F%0Adef%20insertAfter(self%2C%20prev_node%2C%20new_data)%3A%0A%20%0A%20%20%20%20%23%201.%20check%20if%20the%20given%20prev_node%20exists%0A%20%20%20%20if%20prev_node%20is%20None%3A%0A%20%20%20%20%20%20%20%20print%20%22The%20given%20previous%20node%20must%20inLinkedList.%22%0A%20%20%20%20%20%20%20%20return%0A%20%0A%20%20%20%20%23%20%202.%20Create%20new%20node%20%26%0A%20%20%20%20%23%20%203.%20Put%20in%20the%20data%0A%20%20%20%20new_node%20%3D%20Node(new_data)%0A%20%0A%20%20%20%20%23%204.%20Make%20next%20of%20new%20Node%20as%20next%20of%20prev_node%20%0A%20%20%20%20new_node.next%20%3D%20prev_node.next%0A%20%0A%20%20%20%20%23%205.%20make%20next%20of%20prev_node%20as%20new_node%20%0A%20%20%20%20prev_node.next%20%3D%20new_node\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Time complexity of insertAfter() is O(1) as it does constant amount of work.<\/p>\n<p><strong>Add a node at the end: (6 steps process)<\/strong><br \/>\nThe new node is always added after the last node of the given Linked List. For example if the given Linked List is 5->10->15->20->25 and we add an item 30 at the end, then the Linked List becomes 5->10->15->20->25->30.<br \/>\nSince a Linked List is typically represented by the head of it, we have to traverse the list till end and then change the next of last node to new node.<\/p>\n<p><img decoding=\"async\" class=\"alignleft size-full wp-image-26703\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_insert_last.png\" alt=\"C Programming - Inserting a node in Linked List | Set 2\" width=\"759\" height=\"245\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_insert_last.png 759w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_insert_last-300x97.png 300w\" sizes=\"(max-width: 759px) 100vw, 759px\" \/><\/p>\n<p>Following are the 6 steps to add node at the end<\/p>\n<p><strong>Python Programming:<\/strong><\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20This%20function%20is%20defined%20in%20Linked%20List%20class%0A%23%20Appends%20a%20new%20node%20at%20the%20end.%20%20This%20method%20is%0A%23%20%20defined%20inside%20LinkedList%20class%20shown%20above%20*%2F%0Adef%20append(self%2C%20new_data)%3A%0A%20%0A%20%20%20%23%201.%20Create%20a%20new%20node%0A%20%20%20%23%202.%20Put%20in%20the%20data%0A%20%20%20%23%203.%20Set%20next%20as%20None%0A%20%20%20new_node%20%3D%20Node(new_data)%0A%20%0A%20%20%20%23%204.%20If%20the%20Linked%20List%20is%20empty%2C%20then%20make%20the%0A%20%20%20%23%20%20%20%20new%20node%20as%20head%0A%20%20%20if%20self.head%20is%20None%3A%0A%20%20%20%20%20%20%20%20self.head%20%3D%20new_node%0A%20%20%20%20%20%20%20%20return%0A%20%0A%20%20%20%23%205.%20Else%20traverse%20till%20the%20last%20node%0A%20%20%20last%20%3D%20self.head%0A%20%20%20while%20(last.next)%3A%0A%20%20%20%20%20%20%20last%20%3D%20last.next%0A%20%0A%20%20%20%23%206.%20Change%20the%20next%20of%20last%20node%0A%20%20%20last.next%20%3D%20%20new_node\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Time complexity of append is O(n) where n is the number of nodes in linked list. Since there is a loop from head to end, the function does O(n) work.<br \/>\nThis method can also be optimized to work in O(1) by keeping an extra pointer to tail of linked list<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Following is a complete program that uses all of the above methods to create a linked list.<\/strong><\/p>\n<p><strong>Python Programming:<\/strong><\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20A%20complete%20working%20Python%20program%20to%20demonstrate%20all%0A%23%20insertion%20methods%20of%20linked%20list%0A%20%0A%23%20Node%20class%0Aclass%20Node%3A%0A%20%0A%20%20%20%20%23%20Function%20to%20initialise%20the%20node%20object%0A%20%20%20%20def%20__init__(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20self.data%20%3D%20data%20%20%23%20Assign%20data%0A%20%20%20%20%20%20%20%20self.next%20%3D%20None%20%20%23%20Initialize%20next%20as%20null%0A%20%0A%20%0A%23%20Linked%20List%20class%20contains%20a%20Node%20object%0Aclass%20LinkedList%3A%0A%20%0A%20%20%20%20%23%20Function%20to%20initialize%20head%0A%20%20%20%20def%20__init__(self)%3A%0A%20%20%20%20%20%20%20%20self.head%20%3D%20None%0A%20%0A%20%0A%20%20%20%20%23%20Functio%20to%20insert%20a%20new%20node%20at%20the%20beginning%0A%20%20%20%20def%20push(self%2C%20new_data)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%201%20%26%202%3A%20Allocate%20the%20Node%20%26%0A%20%20%20%20%20%20%20%20%23%20%20%20%20%20%20%20%20Put%20in%20the%20data%0A%20%20%20%20%20%20%20%20new_node%20%3D%20Node(new_data)%0A%20%0A%20%20%20%20%20%20%20%20%23%203.%20Make%20next%20of%20new%20Node%20as%20head%0A%20%20%20%20%20%20%20%20new_node.next%20%3D%20self.head%0A%20%0A%20%20%20%20%20%20%20%20%23%204.%20Move%20the%20head%20to%20point%20to%20new%20Node%0A%20%20%20%20%20%20%20%20self.head%20%3D%20new_node%0A%20%0A%20%0A%20%20%20%20%23%20This%20function%20is%20in%20LinkedList%20class.%20Inserts%20a%0A%20%20%20%20%23%20new%20node%20after%20the%20given%20prev_node.%20This%20method%20is%0A%20%20%20%20%23%20defined%20inside%20LinkedList%20class%20shown%20above%20*%2F%0A%20%20%20%20def%20insertAfter(self%2C%20prev_node%2C%20new_data)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%201.%20check%20if%20the%20given%20prev_node%20exists%0A%20%20%20%20%20%20%20%20if%20prev_node%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print%20%22The%20given%20previous%20node%20must%20inLinkedList.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%0A%20%20%20%20%20%20%20%20%23%20%202.%20create%20new%20node%20%26%0A%20%20%20%20%20%20%20%20%23%20%20%20%20%20%20Put%20in%20the%20data%0A%20%20%20%20%20%20%20%20new_node%20%3D%20Node(new_data)%0A%20%0A%20%20%20%20%20%20%20%20%23%204.%20Make%20next%20of%20new%20Node%20as%20next%20of%20prev_node%0A%20%20%20%20%20%20%20%20new_node.next%20%3D%20prev_node.next%0A%20%0A%20%20%20%20%20%20%20%20%23%205.%20make%20next%20of%20prev_node%20as%20new_node%0A%20%20%20%20%20%20%20%20prev_node.next%20%3D%20new_node%0A%20%0A%20%0A%20%20%20%20%23%20This%20function%20is%20defined%20in%20Linked%20List%20class%0A%20%20%20%20%23%20Appends%20a%20new%20node%20at%20the%20end.%20%20This%20method%20is%0A%20%20%20%20%23%20defined%20inside%20LinkedList%20class%20shown%20above%20*%2F%0A%20%20%20%20def%20append(self%2C%20new_data)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%201.%20Create%20a%20new%20node%0A%20%20%20%20%20%20%20%20%23%202.%20Put%20in%20the%20data%0A%20%20%20%20%20%20%20%20%23%203.%20Set%20next%20as%20None%0A%20%20%20%20%20%20%20%20new_node%20%3D%20Node(new_data)%0A%20%0A%20%20%20%20%20%20%20%20%23%204.%20If%20the%20Linked%20List%20is%20empty%2C%20then%20make%20the%0A%20%20%20%20%20%20%20%20%23%20%20%20%20new%20node%20as%20head%0A%20%20%20%20%20%20%20%20if%20self.head%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.head%20%3D%20new_node%0A%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%0A%20%20%20%20%20%20%20%20%23%205.%20Else%20traverse%20till%20the%20last%20node%0A%20%20%20%20%20%20%20%20last%20%3D%20self.head%0A%20%20%20%20%20%20%20%20while%20(last.next)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20last%20%3D%20last.next%0A%20%0A%20%20%20%20%20%20%20%20%23%206.%20Change%20the%20next%20of%20last%20node%0A%20%20%20%20%20%20%20%20last.next%20%3D%20%20new_node%0A%20%0A%20%0A%20%20%20%20%23%20Utility%20function%20to%20print%20the%20linked%20list%0A%20%20%20%20def%20printList(self)%3A%0A%20%20%20%20%20%20%20%20temp%20%3D%20self.head%0A%20%20%20%20%20%20%20%20while%20(temp)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print%20temp.data%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.next%0A%20%0A%20%0A%20%0A%23%20Code%20execution%20starts%20here%0Aif%20__name__%3D%3D\u2019__main__\u2019%3A%0A%20%0A%20%20%20%20%23%20Start%20with%20the%20empty%20list%0A%20%20%20%20llist%20%3D%20LinkedList()%0A%20%0A%20%20%20%20%23%20Insert%206.%20%20So%20linked%20list%20becomes%206-%3ENone%0A%20%20%20%20llist.append(6)%0A%20%0A%20%20%20%20%23%20Insert%207%20at%20the%20beginning.%20So%20linked%20list%20becomes%207-%3E6-%3ENone%0A%20%20%20%20llist.push(7)%3B%0A%20%0A%20%20%20%20%23%20Insert%201%20at%20the%20beginning.%20So%20linked%20list%20becomes%201-%3E7-%3E6-%3ENone%0A%20%20%20%20llist.push(1)%3B%0A%20%0A%20%20%20%20%23%20Insert%204%20at%20the%20end.%20So%20linked%20list%20becomes%201-%3E7-%3E6-%3E4-%3ENone%0A%20%20%20%20llist.append(4)%0A%20%0A%20%20%20%20%23%20Insert%208%2C%20after%207.%20So%20linked%20list%20becomes%201%20-%3E%207-%3E%208-%3E%206-%3E%204-%3E%20None%0A%20%20%20%20llist.insertAfter(llist.head.next%2C%208)%0A%20%0A%20%20%20%20print%20\u2019Created%20linked%20list%20is%3A\u2019%2C%0A%20%20%20%20llist.printList()%0A%20%0A%23%20This%20code%20is%20contributed%20by%20Manikantan%20Narasimhan%0A\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre> Created Linked list is:  1  7  8  6  4<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python Programming &#8211; Time complexity of append &#8211; O(n) where n is number of nodes in linked list. Since there is loop from head to end the function does O(n) <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,79476,4148,79478],"tags":[79729,2870,79728,79721,79723,79720,79724,2874,79719,79726,79727,79725,79722,2869,79730],"class_list":["post-26716","post","type-post","status-publish","format-standard","hentry","category-coding","category-linked-list","category-python","category-singly-linked-list","tag-advantages-and-disadvantages-of-linked-list-over-array","tag-arraylist-vs-linkedlist-c","tag-difference-between-array-and-linked-list-in-data-structure","tag-difference-between-array-and-linked-list-in-java","tag-difference-between-array-and-linked-list-ppt","tag-difference-between-array-and-linked-list-wikipedia","tag-difference-between-array-and-stack","tag-difference-between-arraylist-and-array","tag-difference-between-arraylist-and-linked-list","tag-difference-between-arraylist-and-linkedlist-and-vector-in-java","tag-difference-between-arraylist-and-vector","tag-difference-between-singly-linked-list-and-doubly-linked-list","tag-linked-list-vs-array-performance","tag-what-is-doubly-linked-list-in-java","tag-what-is-the-difference-between-hashset-and-hashmap-classes-in-collection-framework"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26716"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26716\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}