We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defsortedInsert(llist,data):# q - new node q=DoublyLinkedListNode(data)# p - pointer to linked listp=llist# llist is emptyif(notp):returnq# insert at the beginning if(panddata<p.data):q.next=pp.prev=qp=qreturnp# while traversing pointer p while(p):p=p.next# In-Between 2 nodesif(p.data>q.data):p=p.prevq.next=p.nextq.prev=pif(p.next):p.next.prev=qp.next=qreturnllistelse:# inserting at the end of the linked listif(notp.next):p.next=qq.next=Noneq.prev=preturnllistelse:continue
Do Upvote it if you found it helpful & Do Comment below for any suggestions⛳
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Inserting a Node Into a Sorted Doubly Linked List
You are viewing a single comment's thread. Return to all comments →
Python 3 code