{"id":1574,"date":"2018-05-14T19:56:59","date_gmt":"2018-05-14T14:26:59","guid":{"rendered":"http:\/\/pramodsingla.com\/?p=1574"},"modified":"2018-05-14T19:56:59","modified_gmt":"2018-05-14T14:26:59","slug":"python-playing-with-linked-list","status":"publish","type":"post","link":"https:\/\/pramodsingla.com\/?p=1574","title":{"rendered":"Python : Playing with Linked list"},"content":{"rendered":"<ul>\n<li>\n<h2><strong>Create linked list and add node at the beginning<\/strong><\/h2>\n<\/li>\n<\/ul>\n<blockquote>\n<h2>class node:<\/h2>\n<h2>\u00a0\u00a0\u00a0 def __init__(self, p_data):<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.data=p_data<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.next=None<\/h2>\n<h2><\/h2>\n<h2>class linkedlist:<\/h2>\n<h2>\u00a0\u00a0\u00a0 def __init__(self):<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.head=None<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.len=0<\/h2>\n<h2>\u00a0\u00a0\u00a0 def addNodeAtBegin(self,data):<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 newNode=node(data)<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 newNode.next=self.head<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.head=newNode<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.len=self.len+1<\/h2>\n<h2>\u00a0\u00a0\u00a0 def print(self):<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 node=self.head<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 while node:<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(node.data)<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 node=node.next<\/h2>\n<h2><\/h2>\n<h2>newlinkedList=linkedlist()<\/h2>\n<h2>newlinkedList.addNodeAtBegin(10)<\/h2>\n<h2>newlinkedList.print()<\/h2>\n<\/blockquote>\n<p>&nbsp;<\/p>\n<ul>\n<li>\n<h2><strong>Linked list and add node in sorted list<\/strong><\/h2>\n<\/li>\n<\/ul>\n<blockquote>\n<h2>class node:<\/h2>\n<h2>\u00a0\u00a0\u00a0 def __init__(self, p_data):<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.data=p_data<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.next=None<\/h2>\n<h2><\/h2>\n<h2>class linkedlist:<\/h2>\n<h2>\u00a0\u00a0\u00a0 def __init__(self):<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.head=None<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.len=0<\/h2>\n<h2>\u00a0\u00a0\u00a0 def addNodeAtBegin(self,data):<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 newNode=node(data)<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 newNode.next=self.head<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.head=newNode<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.len=self.len+1<\/h2>\n<h2><\/h2>\n<h2>\u00a0\u00a0\u00a0 def sortedInsert(self,data):<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 newNode=node(data)<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Curr=self.head<\/h2>\n<h2><\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if Curr==None or Curr.data&gt;newNode.data:<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 newNode.next=self.head<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.head=newNode<\/h2>\n<h2><\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else:<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Next=Curr.next<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 while Curr.data&lt;=newNode.data:<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if Curr.next==None:<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Curr.next=newNode<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 newNode.next=None<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if Curr.data&lt;=newNode.data and Next.data&gt;=newNode.data:<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Curr.next=newNode<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 newNode.next=Next<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.len=self.len+1<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0Curr=Next<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Next=Next.next<\/h2>\n<h2>\u00a0\u00a0\u00a0 def print(self):<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 node=self.head<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 while node:<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(node.data)<\/h2>\n<h2>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 node=node.next<\/h2>\n<h2><\/h2>\n<h2>newSortedlinkedList=linkedlist()<\/h2>\n<h2>newSortedlinkedList.sortedInsert(10)<\/h2>\n<h2>newSortedlinkedList.sortedInsert(20)<\/h2>\n<h2>newSortedlinkedList.sortedInsert(30)<\/h2>\n<h2>newSortedlinkedList.sortedInsert(15)<\/h2>\n<h2>newSortedlinkedList.sortedInsert(10)<\/h2>\n<h2>newSortedlinkedList.sortedInsert(30)<\/h2>\n<h2>newSortedlinkedList.sortedInsert(40)<\/h2>\n<h2>newSortedlinkedList.sortedInsert(5)<\/h2>\n<h2><\/h2>\n<h2>newSortedlinkedList.print()<\/h2>\n<\/blockquote>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Create linked list and add node at the beginning class node: \u00a0\u00a0\u00a0 def __init__(self, p_data): \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.data=p_data \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self.next=None class[&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[17,26],"tags":[103,107],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pramodsingla.com\/index.php?rest_route=\/wp\/v2\/posts\/1574"}],"collection":[{"href":"https:\/\/pramodsingla.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pramodsingla.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pramodsingla.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pramodsingla.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1574"}],"version-history":[{"count":0,"href":"https:\/\/pramodsingla.com\/index.php?rest_route=\/wp\/v2\/posts\/1574\/revisions"}],"wp:attachment":[{"href":"https:\/\/pramodsingla.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pramodsingla.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pramodsingla.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}