doubly linked list in python

Solutions on MaxInterview for doubly linked list in python by the best coders in the world

showing results for - "doubly linked list in python"
Greta
25 Oct 2018
1# Initialise the Node
2class Node:
3    def __init__(self, data):
4        self.item = data
5        self.next = None
6        self.prev = None
7# Class for doubly Linked List
8class doublyLinkedList:
9    def __init__(self):
10        self.start_node = None
11    # Insert Element to Empty list
12    def InsertToEmptyList(self, data):
13        if self.start_node is None:
14            new_node = Node(data)
15            self.start_node = new_node
16        else:
17            print("The list is empty")
18    # Insert element at the end
19    def InsertToEnd(self, data):
20        # Check if the list is empty
21        if self.start_node is None:
22            new_node = Node(data)
23            self.start_node = new_node
24            return
25        n = self.start_node
26        # Iterate till the next reaches NULL
27        while n.next is not None:
28            n = n.next
29        new_node = Node(data)
30        n.next = new_node
31        new_node.prev = n
32    # Delete the elements from the start
33    def DeleteAtStart(self):
34        if self.start_node is None:
35            print("The Linked list is empty, no element to delete")
36            return 
37        if self.start_node.next is None:
38            self.start_node = None
39            return
40        self.start_node = self.start_node.next
41        self.start_prev = None;
42    # Delete the elements from the end
43    def delete_at_end(self):
44        # Check if the List is empty
45        if self.start_node is None:
46            print("The Linked list is empty, no element to delete")
47            return 
48        if self.start_node.next is None:
49            self.start_node = None
50            return
51        n = self.start_node
52        while n.next is not None:
53            n = n.next
54        n.prev.next = None
55    # Traversing and Displaying each element of the list
56    def Display(self):
57        if self.start_node is None:
58            print("The list is empty")
59            return
60        else:
61            n = self.start_node
62            while n is not None:
63                print("Element is: ", n.item)
64                n = n.next
65        print("\n")
66# Create a new Doubly Linked List
67NewDoublyLinkedList = doublyLinkedList()
68# Insert the element to empty list
69NewDoublyLinkedList.InsertToEmptyList(10)
70# Insert the element at the end
71NewDoublyLinkedList.InsertToEnd(20)
72NewDoublyLinkedList.InsertToEnd(30)
73NewDoublyLinkedList.InsertToEnd(40)
74NewDoublyLinkedList.InsertToEnd(50)
75NewDoublyLinkedList.InsertToEnd(60)
76# Display Data
77NewDoublyLinkedList.Display()
78# Delete elements from start
79NewDoublyLinkedList.DeleteAtStart()
80# Delete elements from end
81NewDoublyLinkedList.DeleteAtStart()
82# Display Data
83NewDoublyLinkedList.Display()
Sophie
08 Jul 2019
1class Node:
2    def __init__(self, data = None, next_node = None):
3        self.data = data
4        self.nextNode = next_node
5
6    def get_data(self):
7        return self.data
8
9    def set_data(self, data):
10        self.data = data
11
12    def get_nextNode(self):
13        return self.nextNode
14
15    def set_nextNode(self, nextNode):
16        self.nextNode = nextNode
17
18
19class LinkedList:
20    def __init__(self, head = None):
21        self.head = head
22
23
24    def add_Node(self, data):
25        # if empty
26        if self.head == None:
27            self.head = Node(data)
28
29
30        # not empty
31        else:
32            curr_Node = self.head
33            
34            # if node added is at the start
35            if data < curr_Node.get_data():
36                self.head = Node(data, curr_Node)
37                
38            # not at start
39            else:
40                while data > curr_Node.get_data() and curr_Node.get_nextNode() != None:
41                    prev_Node = curr_Node
42                    curr_Node = curr_Node.get_nextNode()
43
44                # if node added is at the middle
45                if data < curr_Node.get_data():
46                    prev_Node.set_nextNode(Node(data, curr_Node))
47                
48
49                # if node added is at the last
50                elif data > curr_Node.get_data() and curr_Node.get_nextNode() == None:
51                    curr_Node.set_nextNode(Node(data))
52
53
54
55    def search(self, data):
56        curr_Node = self.head
57        while curr_Node != None:
58            if data == curr_Node.get_data():
59                return True
60
61            else:
62                curr_Node = curr_Node.get_nextNode()
63
64        return False
65
66
67    def delete_Node(self, data):
68        if self.search(data):
69            # if data is found
70
71            curr_Node = self.head
72            #if node to be deleted is the first node
73            if curr_Node.get_data() == data:
74                self.head = curr_Node.get_nextNode()
75
76            else:
77                while curr_Node.get_data() != data:
78                    prev_Node = curr_Node
79                    curr_Node = curr_Node.get_nextNode()
80                    
81                #node to be deleted is middle
82                if curr_Node.get_nextNode() != None:
83                    prev_Node.set_nextNode(curr_Node.get_nextNode())
84
85                # node to be deleted is at the end
86                elif curr_Node.get_nextNode() == None:
87                    prev_Node.set_nextNode(None)
88
89        else:
90            return "Not found."
91
92    def return_as_lst(self):
93        lst = []
94        curr_Node = self.head
95        while curr_Node != None:
96            lst.append(curr_Node.get_data())
97            curr_Node = curr_Node.get_nextNode()
98
99        return lst
100
101    def size(self):
102        curr_Node = self.head
103        count = 0
104        while curr_Node:
105            count += 1
106            curr_Node = curr_Node.get_nextNode()
107        return count
108
109      
110## TEST CASES #
111test1 = LinkedList()
112test2 = LinkedList()
113test1.add_Node(20)
114test1.add_Node(15)
115test1.add_Node(13)
116test1.add_Node(14)
117test1.delete_Node(17)
118print(test1.return_as_lst())
119print(test2.size())
Lia
15 Jan 2020
1class doublelinkedlist{  
2    Node head;
3    Node tail;
4
5    static class Node{
6        int data;
7        Node previous;
8        Node next;
9
10        Node(int data) { this.data = data; }
11    }
12
13    public void addLink(int data) {
14        Node node = new Node(data);
15
16        if(head == null) {
17            head = tail = node;
18            head.previous = null;
19        } else {
20            tail.next = node;
21            node.previous = tail;
22            tail = node;
23        }
24        tail.next = null;
25    }
26}
Alonso
10 May 2019
1class ListNode:
2    def __init__(self, value, prev=None, next=None):
3        self.prev = prev
4        self.value = value
5        self.next = next
6
7class DoublyLinkedList:
8    def __init__(self, node=None):
9        self.head = node
10        self.tail = node
11        self.length = 1 if node is not None else 0
12
13    def __len__(self):
14        return self.length
15   
16  	def add_to_head(self, value):
17        new_node = ListNode(value, None, None)
18        self.length += 1
19        if not self.head and not self.tail:
20            self.head = new_node
21            self.tail = new_node
22        else:
23            new_node.next = self.head
24            self.head.prev = new_node
25            self.head = new_node
26 
27    def remove_from_head(self):
28        value = self.head.value
29        self.delete(self.head)
30        return value
31
32    def add_to_tail(self, value):
33        new_node = ListNode(value, None, None)
34        self.length += 1
35        if not self.tail and not self.head:
36            self.tail = new_node
37            self.head = new_node
38        else:
39            new_node.prev = self.tail
40            self.tail.next = new_node
41            self.tail = new_node
42            
43
44    def remove_from_tail(self):
45        value = self.tail.value
46        self.delete(self.tail)
47        return value
48            
49    def move_to_front(self, node):
50        if node is self.head:
51            return
52        value = node.value
53        if node is self.tail:
54            self.remove_from_tail()
55        else:
56            node.delete()
57            self.length -= 1
58        self.add_to_head(value)
59        
60    def move_to_end(self, node):
61        if node is self.tail:
62            return
63        value = node.value
64        if node is self.head:
65            self.remove_from_head()
66            self.add_to_tail(value)
67        else:
68            node.delete()
69            self.length -= 1
70            self.add_to_tail(value)
71
72    def delete(self, node):
73        self.length -= 1
74        if not self.head and not self.tail:
75            return
76        if self.head == self.tail:
77            self.head = None
78            self.tail = None
79        elif self.head == node:
80            self.head = node.next
81            node.delete()
82        elif self.tail == node:
83            self.tail = node.prev
84            node.delete()
85        else:
86            node.delete()
87
88    def get_max(self):
89        if not self.head:
90            return None
91        max_val = self.head.value
92        current = self.head
93        while current:
94            if current.value > max_val:
95                max_val = current.value
96            current = current.next
97        return max_val
queries leading to this page
linkedlist using pythoninsertion all in doubly linked list in c 2b 2blinked list pythontranserve 2c delete 2c insert on doubly linked list and print it in javausing linked list in pythonwhen to use linked list over python listlinked list operation in pythoninsert into doubly linked listwhat is java double linked listpython implement a linked list codeis there linked list in python doubly linked list c 2b 2bdoes python use linked listsis list in python a linked listdoubly linked list implementation in pythonlinked list example pythonlinked listpython3 linked listlinked and doubly linked listhow to set the head of a doubly linked list pythonlnked list defautl data structrei n pythonwhat does a linked list output look like in python for testlinled linsertions python algorithmcreation of linked list in pythonhow to values of a linked are in another linked list pythonlinked chain implementation in pythondoubly linked list python example simplehow to make linked list in pythonlinked list tutorial pythonhow to code a linked list in pythondouble linked lists javadoublelinkedlist in javadoubly linked list class implementation what can be done in o 281 29 in doubly linked listlinked list library in pythondoubly linked list implementation in javawhat is linked list in pythonpython how to create a linked listlearn linked lists pythonat the frint doublylinked list javadll traversal function from the endhow to work with linked lists in pythondoubly linked list diagramfor linked list in pythonhow to make a linked lit in python with pointerscreate a linkedlist in pythonlinked lists in pythonhow to set the head of a doubly linked listdoubly list in javause doubly linked list java collectionsingly linked operations in data structure code pythonlinked list python tutorialadd a node double linked list in pythonpython program for doubly linked listlinked list code pythonc 2b 2b linked listlinked list python example codeinsert doubly linked list javajava double linked listjava jdk doubly linked listlinked list all operations in pythonpython code for doubly linked listlinkd list pythonlist python linked listis java linked list doubly linkedhow useful are linked lists in pythonhow to create linked list pythonhow to use linked list in pythonlinkedlist pytho codedoubly linked list in java built in implementationlinkedlist python codelinked list pythoimplementation of a doubly linked list in pythondoubly linked list data structure in javalinked list python in detailare java linked list single or doubledoubly linked list java collectionlinked lists python 3linked list pthonpython tutor linked listdouble linkedin list in javalinked list i pythonwhat are linked lists 3f called in pythonentry in a dobe neded link listlinkelist in pythonwhat are linked list pyhtonpython program to implement doubly linked listhow to create linked list in pythonhow to implement a doubly linked listhow to display pointers in a linked list in pythonlinked list code in pythonappend doubly linked list javadoubly linked list insert python implementationlist in python is singly linkedlinked list in python exampledoubly linked list implementationin javapython class linked listpythonlinked lists exampledoubly linkedlist in javalinked list in python implementationpython for in linked listpython chained linked listdll linked liststore a number in linked list pythonwrite a java program to create a doubly linked list of elementspython return linked listpython doubly linked list implementationdoes python have linked listpython linked lists tutorialpython create linked listlinked list pythindoubly linked list operations program in pythonis a python list a linked listinsert method in java doubly linked listdouble linked list in javaimplementing doubly linked list in pythonimplement a doubly linked list in c 2b 2bdoes python have linked listsimplementing linked list in pythondoes python have a built in linked listdata structures and algorithms linked list pythonlinked lists in python noteslinklists in pythonpython list or linked listlinked list implementation using pythonadd a node before p in doubly linked listlinked list pytonlinked list python libraryimplementation of double linked list in javalinked list using list in pythoncreate alinked list inb pyhtonpython linkeidn list double linked list codedoubly linked list in java without collectionimplement linked list in pythondouble link list javadoubly linked list python libraryhow to define a linked list in pythondoubly linked list insert at implementationpython linked list built indoubly link list l l head 3e next in doubly linked 2c null value is inpython linked list examplebidirectional linked list javahow to linked lists together pythondoubly linked list in data structure using pythonpython linked list how toare list in python linked listshow to create a linked list in python3linked lists with pythondoubly linked list python gfglinked list python programis linked list in pythondouble linked listcreate doubly linked listdoubley linkedlist in javawhat is a linked list python 3fimport linked list pythonhow to make linked list on pythonpythonn linked listdoubly linked list in java e2 80 93 implementation 26 codedoubly linked list class javainsert iterm using double linked listusing python to define a linked listinsert function doubly linked list c 2b 2bdoubly linked list implementation java codebulilt in double linkedlist in javaone way linked list pythonpython linked listhow to diagram a doubly linked list pythondefinition of a function in python linked listpython lists are linked listsdoubly linked list using pythonwhen to use linked list in pythonlinked list java doubly linkedpublic class doubly linked listin linked list pythonpython linked list librarycode for creating linked list in pythoninbuilt function for head for linked list in pythonlinked lists python3doubly linked list in pythoncreate a double linked list of size n where the information part of each node contain an integercreate linked list pythonlinked list python usesdoubly linked list impementationcircular linked list cpphow to create linked list in pythonwhat is a linked list pythonlinkedlist function in pythonbuilt in linked list pythonlink and linked list in pyhtondifferent ways to implement doubly link listdoubly linked list in python 3queue linked list pythonimplementation of doubly linked listpython doubly linked listdoubly link list in pythonpython linked list methodslinked list pythonjava double linked list adddoubly linked list integer javalinked list implementation inpythonlinked list python linked listhow to fail linked list contains in pythontraverse linked list pythonlinked list on pythondoubly linked list python codeare linked lists used in pythonhow to define a linked list pythondefine a loinked list in python python doubly linked list packagehow to use a linked list in pythonlinked list method in pythonhow to create a double linked list in javasigly linked list in pythonare python lists linked listsdoubly linked nullhow to do linked list in pythondouble linked list python rear insertgeeks java double linked listdoubly linked list all operations in pythonpython program for linked listlinked list implementation pythonpython linked code pythonlinked list traversal pythonlinked list in pythonlinked lists in python 3linked list program in python whole codelinked list program in pythoncreating a linked list pythonlinked list methods pythonjava abstract doubly linked listtwo way linked listhow to do linked lists work in pythonhow to define linked list in python linked list pythonpopulating an empty linked list in pythonpython code linkedlistjava program to implement doubly linked listdoubly linked list javatranserve 2c delete 2c insert on doubly linked listin javalinked list python exampledouble linked list pyhtonlinked list in python using listlistnode in pythonlinked list insertions python algorithmjava doubly linked listpython source code linked listlinked list in python getexamples of sanity code java for a doubly linked listsingle linked list in pythonpython make a doubly linked listadding to double linked listhow to create a doubly linked list in pythonpython new linked listpython linkedlist builtfind doubly linked list pythonhow to make linked list object pythonlinked list python implementationlinkedin list pythondifferent ways to implement doubly linked listdoubly linked lists pythondoublly linked listlinked list iin pythonlinkedlist 28 29 function in pyhtonlinked list python modulelinked list pyhtontraverse linked list in pythonsingly linked list pythonwhat is the linked list in pythonimplement doubly linked list in pythonhow do linked lists work pythonlinked list in python usesdouble linked list pythonpython linked list java doubly linked list reach elementpython linked list how to keep track of headdoubly node linked listdoubly linked list in javaread linked list pythonhow to code an insert after for a doubly linked list javashould i do linked list in pythonpython how to do linked listlinked list in python3create linkedlist using list pythonlinkedlists pythonprogram to implement doubly linked list in pythondoublly linked lists java explainedhow to traverse a linked list in pythonpython built in linked listhow to create a node in linked list pythonadding to the front of a double linked list javaimplementing a linked list in pythonsingly linked list pythonimplement a linked list in pythonbhow to get the value at the end of a doubly linked listcreate a double linked list 2c wherein the data elements are either multiple of 5 or 7 and less than 99 hence write a code snippet to implement this linked listpython insert into linked list return whole listdoubly linked list in data structure pythonsingle linked list python exampledoubly linked list implementation easier 3fadd to doubly linked listwhat is doubly linked list doubly linked listlinked list in pythonhow to create a linked list in pythoncreate a double linked list 2c wherein the data elements 28integer values 29 are either multiple of 5 or 7 and less than 99 hence write a code snippet to implement this linked listdouble linked list explained javaimplementation of doubly linked implementationdouble link list pythonadd to head on a doubly linked list in pythonlinked list in pytonlinked list in python with explanationsingly linked list python examplebound to a list python what isimplementing doubly linked linked list add methoddefine a linked list in pythonlinked list package pythonwhat is a linked list in pythonimplement a linked list in pythonlinked list in pythoon in python linked listnode class of doubly linked listpython list is linked listdoes python have a built in linkedlistlinked list in python meaninghow to code a simple linked list in pythonhowto linked list in python how to build linked list with a list pythonlist de link pythonimplement a doubly linked listdouble linked listsuse doubly linked lists javalinked list in pthondoubly linked list struct inserting three vsaluesunderstanding linked lists pythonuses of linkedlist in pythonlinked list python methodslinked list pythnodoubly linked list pythondoubly linked list python favtutorlearn how to implement linked list in pythondoubly linked list java codepython create a simple linked listhow to make a linked list in pythonpython linkedlist librarylinked list in python tutorialpoinntwhat are linked lists in pythoninsertion into doubly linked listesculator functioning in doubly linked listhow to declare a doubly linked list in javapython program using linked listtranserve 2c delete 2c insert on doubly linked list in javapython linkedlist implementationlinked lists pythonhow do linked lists work in pythoncreate linked list in pythondo python list act as linked listhow to do a linked list in pythonlinkedin list in pythondoubly linked list node javapython linked list head methodpython linked list functionscreating doubly linked list using clasesdoublelinked list in javalist nodes in pythondoubly linked lists in pythonhow to import linked list in pythonlinked lists in python3linked list in python best explanationa simple doubly linked listlinked list in pyimplementation of all the methods in linkedlist in pythoncreate a doubly linked list in pythonlinked list python explainedhow to work with linked lists pythonmake a linked list in pythonlinkedin python libraryjava create your own double linked listwhat does a linked list look like in pythonlinked list python structurepython linked lists examplelinked list python3doubly linked list adt javaappend doubly linked listlinked list pythonpython linked listslinked list python codechained list pythondoubly linked linked listdoubly linked list adding node at defined positionadding a node to the end of a dlladding a node to the front of a doubly linked list javalinkedin list pythontwo way linked list in chow create linked list in pythondoubly linked list using javaimplementing a doubly linked listcreate a linked list pythoncreate a doubly linked list in javahow to create a new linked list pythonlinked list pythonnhow to link lists together pythonpython linked list implementationsimple program to implement doubly linked list in pythoninitilize linked list pythonpython linked listsdoubly linked list implementation in c 2b 2bpython linked list sequencedouble linked list javalinked lsts in pythonpython example linkedlisthow to make linked lists pythoncreate linked list by pythonlinked list pyrhonpython doubly link listdoubly linked list in data structure in pythonlinked list pythonlink list in pythonpython linkedinlisthow to avoid making a doubly linked listlinked list object pythonwhat is a linked list pythoninitialize linked list node in pythonlinkedlist python tutorlinkjed list pythonpython why use linked listlist implementation in pythonlinked lists and how they work pythondoubly linked list add methodget linked list python methoddoubly linked list code in javapython doubly linked list libraryis python list a linked listlinked list pytohn 5dlinke list in pythonprogram for creation insertion and deletion in doubly linked listmethods of doubly linked listtraverse a linked list pythonpackage doubly linked list in javalinkedlist in pythonpython library linkedlistdoubly linked list importlinked list sing pythondoubly linkedlist pythonlinked list in pythoinlinked list inpythoninsert front doubly linked list javabuilt doubly linked list pythonhow many null pointers a doubly linked list can have at minimum 3fare linked lists needed in pythonhow to insert data in doubly linked listpython linked listlinked list pythobsyntax of create linked list in pythonlinked list package in pythonlinked list operations in pythonlinked list in python orglinked list algorithm in pythonpython adding t linked listslinked list implementation in pythonlinked list using pyhonusing linked list directly in pythonfunction that prints all the elements of a double linked list in c language linked list in python 3linked list traversal in pythonhow to return the head of a linked list in pythonlinked list in python libraryat the frint doublylinked listdoubly linked listin javapython double linked listhow linked list used for in programming in phythonlink list pythonlinkedlist implementtation pythonpython implement linked listinsert item into double linked listnext val linkied listhow to use builtin linked list in pythonwill java collect a last item of doubly linked list if last item is pointing to previous buyt previous pointtin to nulldraw double linked list pythonlinkedlist pythonpy linked listlinked lists python codepython library for linked listinsertion in doubly linked list in c 2b 2bis python list based on linked listself in linkedlist pythonlist node pythonpython linked list builtindobly linked list in python swhere are linked lists used pythonimplement linkedlist pythonimplement linked list in python includepython traverse a linked listdouble linked list in pythonlinked list in python program implement linked list pythondobuly linked list in pythonadd methods for double link list with multiple cases javaaccesing linked list pythonimplementation for a doubly linked listdevelop a program to implemetn following operations on a doubly linked list 3a i 29 insert before a given valuedoubly linked list python mediumtraversing a linked list pythonusing linked lists pythonwhat are linked lists pythonbasic linkedlist operations in pythonlinked list using pythonjava print doubly linked listsingly linked list using pythonwhat is doubly linked list in pythonjava abstract doubly linked list is emptylist to linked list pythonpython inbuilt linkedlistlinked list node class pythonlinkedlists in pythondoubly linked list complete implementationsingly linked list pythinsingly linked list program in pythonstructure for double linked listcreate double linked listjavalinked lisy pythonmake linked list in pythonlinked list implementation in pythonadvantages of linked lists in pythonwhat is linked list pythondoubly linked list in python in pythonpython linkedlistis python list linkedhow to code doubly linked listcreate a double linked list of size n where the information part of each node contain an integer now add the info of the first node with the last nodelinked list python guidedisplay linked list in pythondoubly linked list input restrectiondefine linked list in pythonlinked list pysethead double llinkedlistcreate doubly linked list in pythonpython implement a linked list linked list python with methodslinklist pythonpython data structures doubledouble linked list using javalinked llist in pythonpython linked list gfgpython linked list codedoubly linked list insert question compilerdoubly linked list python implementationtraversing over doubly linked listtwo way linked list diagramdouble linked list implement alinkedinlist python codedoublelynkedlist javalinked lsit in pythondoubly linked list in c 2b 2blinked list with pythondoubly linked list javadoubly linked list java implementationdouply linked list in pythonhow to make a doubly linked list javacreate a doubly linked list of elements create linked list from list pythondoubly linked list in python