linked list python

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

showing results for - "linked list python"
Erika
08 Nov 2017
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())
Daniela
11 Aug 2018
1# Linked list operations in Python
2
3
4# Create a node
5class Node:
6    def __init__(self, data):
7        self.data = data
8        self.next = None
9
10
11class LinkedList:
12
13    def __init__(self):
14        self.head = None
15
16    # Insert at the beginning
17    def insertAtBeginning(self, new_data):
18        new_node = Node(new_data)
19
20        new_node.next = self.head
21        self.head = new_node
22
23    # Insert after a node
24    def insertAfter(self, prev_node, new_data):
25
26        if prev_node is None:
27            print("The given previous node must inLinkedList.")
28            return
29
30        new_node = Node(new_data)
31        new_node.next = prev_node.next
32        prev_node.next = new_node
33
34    # Insert at the end
35    def insertAtEnd(self, new_data):
36        new_node = Node(new_data)
37
38        if self.head is None:
39            self.head = new_node
40            return
41
42        last = self.head
43        while (last.next):
44            last = last.next
45
46        last.next = new_node
47
48    # Deleting a node
49    def deleteNode(self, position):
50
51        if self.head is None:
52            return
53
54        temp = self.head
55
56        if position == 0:
57            self.head = temp.next
58            temp = None
59            return
60
61        # Find the key to be deleted
62        for i in range(position - 1):
63            temp = temp.next
64            if temp is None:
65                break
66
67        # If the key is not present
68        if temp is None:
69            return
70
71        if temp.next is None:
72            return
73
74        next = temp.next.next
75
76        temp.next = None
77
78        temp.next = next
79
80    # Search an element
81    def search(self, key):
82
83        current = self.head
84
85        while current is not None:
86            if current.data == key:
87                return True
88
89            current = current.next
90
91        return False
92
93    # Sort the linked list
94    def sortLinkedList(self, head):
95        current = head
96        index = Node(None)
97
98        if head is None:
99            return
100        else:
101            while current is not None:
102                # index points to the node next to current
103                index = current.next
104
105                while index is not None:
106                    if current.data > index.data:
107                        current.data, index.data = index.data, current.data
108
109                    index = index.next
110                current = current.next
111
112    # Print the linked list
113    def printList(self):
114        temp = self.head
115        while (temp):
116            print(str(temp.data) + " ", end="")
117            temp = temp.next
118
119
120if __name__ == '__main__':
121
122    llist = LinkedList()
123    llist.insertAtEnd(1)
124    llist.insertAtBeginning(2)
125    llist.insertAtBeginning(3)
126    llist.insertAtEnd(4)
127    llist.insertAfter(llist.head.next, 5)
128
129    print('linked list:')
130    llist.printList()
131
132    print("\nAfter deleting an element:")
133    llist.deleteNode(3)
134    llist.printList()
135
136    print()
137    item_to_find = 3
138    if llist.search(item_to_find):
139        print(str(item_to_find) + " is found")
140    else:
141        print(str(item_to_find) + " is not found")
142
143    llist.sortLinkedList(llist.head)
144    print("Sorted List: ")
145    llist.printList()
Felipe
22 Feb 2016
1pip install llist
Matthew
02 Apr 2016
1Full Linked List implementation at this link:
2https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/LinkedList
Teddy
01 Aug 2019
1
2class Node:
3    def __init__(self, data=None):
4        self.data = data
5        self.next = None
6
7class SLinkedList:
8    def __init__(self):
9        self.head = None
10
11    def Atbegining(self, data_in):
12        NewNode = Node(data_in)
13        NewNode.next = self.head
14        self.head = NewNode
15		
16# Function to remove node
17    def RemoveNode(self, Removekey):
18
19        HeadVal = self.head
20
21        if (HeadVal is not None):
22            if (HeadVal.data == Removekey):
23                self.head = HeadVal.next
24                HeadVal = None
25                return
26
27        while (HeadVal is not None):
28            if HeadVal.data == Removekey:
29                break
30            prev = HeadVal
31            HeadVal = HeadVal.next
32
33        if (HeadVal == None):
34            return
35
36        prev.next = HeadVal.next
37
38        HeadVal = None
39
40    def LListprint(self):
41        printval = self.head
42        while (printval):
43            print(printval.data),
44            printval = printval.next
45
46
47llist = SLinkedList()
48llist.Atbegining("Mon")
49llist.Atbegining("Tue")
50llist.Atbegining("Wed")
51llist.Atbegining("Thu")
52llist.RemoveNode("Tue")
53llist.LListprint()
Simona
19 Mar 2017
1
2class Node:
3    def __init__(self, val):
4        self.val = val
5        self.next = None
6    
7    def nexts(self, nexts):
8        self.next = nexts
9        
10        
11    def __repr__(self):
12        return f'{self.val} -> {self.next} '
13      
14      
15class LinkedList:
16    def __init__(self):
17        self.head = None
18    
19    def add(self, val):
20        if self.head == None:
21            self.head = val
22        else:
23            val.next = self.head
24            self.head = val
25    
26    def __repr__(self):
27        return self.head.__repr__()
28    
29    def __iter__(self):
30        li = self.head
31        while  li.next != None:
32            yield li.val
33            li = li.next
34        yield li.val
35    
36    def  __getitem__(self, n):
37        if n==0: return self.head.val
38        li = self.head
39        for i in range(n):
40            li = li.next
41        return li.val
42    
43    def __len__(self):
44        for i,v in enumerate(self):
45            pass
46        return i+1
47
queries leading to this page
get 28 29 linked list pythonwhat are linked lists 3f called in pythonlinked list implementation in pythonhow to use linked lists pythonpythonds basic linked listlinked list in pypython linked list nodelinke list pythihow do i do python linked listlink list in pythonpython linked list methodsimplement a linked list in pythonnode list class pythonpython linked listpython linked lists tutorialpython linked list in pythonlinked list library in pythonpython library for linked listinbuilt function for head for linked list in pythonlinked list in python tutorialpoinntlinked list by functions in pythonhow to create a new linked list pythonlinked list operation in pythonlinked lists in python noteswhen to use linked list over python listlinked lsit in pythonlinked list in python3python singly linked listis python list linkedlinkedlst in pythonhow to create linked list in pyhtonlist implementation in pythonpython linked list head methodlinked lists in python 3create linked list pythonlinked list node in pythonpython as linked listpython new linked listcan i use linked lists in pythonlinked list using pythonsingly linked list pythinpython linked list built inpython code for singly linked listlearn linked list pythondefine linked list in pythonpython traverse a linked listlinked list node class pythonlinked list pthonlinkedlists in pythonlinked list methods in python w2python implement a linked list linked list pyrhonpython liked list codepython insert into linked list return whole listhow to do linked list in pythonpython create a linked list with a listpython linked lsitlinked list in python meaningnext val linkied listlinked list in django tutorialspointlinked list in python programizlinkedlist pythonlinkedlist in python very simplepython linked list examplewhat does a linked list output look like in python for testimplementing a linked list in pythonlinkedlist 28 29 function in pyhtonhow to use a linked list in pythonlinked list operations in pythonlist and linked list in pythonpython make a linked listlinked list class pythonlinked list prev in pythonlinked lists python 3create linkedlist using list pythonhow to import linked list in pythondata structures linked list pythonis it easy to learn linked list in pythonworking with linked lists in pythonlinked lists in python3how to work with linked lists in pythoncreate alinked list inb pyhtonhow to create linked list in pythonhow to syntax create linked list in pythonlinked lists python3linked list in python 3linked list itr pythonpython program using linked listpython linkedlistpython 3 linked list functionscondense linked list pythonpython linked list librarylinked list in pythoondefinition of a function in python linked listwhat does a linked list look like in python linked list pythonsingly linked list pythonif self head linkedlist pythonpython linked list functionslinked list and its operations code in pythonlinked list traversal pythonlinked list in python examplehow to make a linked list in pythondoes python have linked listspython linkedlist librarylinked list in python best explanationshould i do linked list in pythonexample of linked list in pythonhow to define a linked list pythonlinked list pythobhow create linked list in pythonpython module for creating linked listcode a linked list pythonlinked list in python implementationlinked list and machine learning pythonsingly linked list program in pythonhow to linked lists together pythonmake linked list in pythonare list in python linked listspython chained linked listlinked list on pythonlinked list setup pythonhow to linked list different from normal list pythonlinked list implementation in pythonhow do linked lists work in pythontraversing a linked list pythonlinked lists with pythonlinked list pywhat is the linked list in pythonlinked list in pythonlinked list python in detailcreate a linkedlist in pythonin linked list pythoncreate linked list in pythonpython list is linked listlinked list pyhtonlinked list python librarytraverse linked list in pythonlinked list methods pythondeclaring a linked list in pythoncode for creating linked list in pythonhow to make a linked lit in python with pointershow to create a linked list in python3same object in linked list pyhtonhow to make a linked list in pytohnusing linked list directly in pythonlinked list functions in pythonlinked list i pythonpython tutor linked listpython function linked listnode in pythonlinkedinlist python codelinked list in python for beginnerstraversing singly linked list pythonlinkedlist python full codemake a list a linked list pythonself in linkedlist pythonlinked list tutorial pythondoes python have a built in linked listdoes python use linked listslinkedlists library in pythonpython linked listlinked list example pythonhow to work with linked lists pythonpython build a linked listpython linked list insertlinked list code in pythonimplementation of all the methods in linkedlist in pythonlinkjed list pythonhow to create a list from a linked list pythonpython linked list sequenceinserting data structure list in flask sqlalchemypython linked list structurelinked list algorithm in pythonlinked lit class in pythonpython linkedlist nodeshow to fail linked list contains in pythonchained list pythonlist python linked listwhat is a linked list pythonlinked listlinked lists neet lecture notes python linked list traversal in pythonall functions you can do to linked lists pythoncreate new node in linked list pyhtonpython store number as linked listpython inked listlinkedlist in pythonlist to linked list pythonlinkedlist python tutorpython linkeidn list linked list python codelinked list python linked listpython program to create linked listlinked list example pytohnis a python dictionary a linked listpopulating an empty linked list in pythonlist node pythonis linked list is useful for pythoninitilize linked list pythonbuilt in linked list pythonpython linked list how to keep track of headhow to return the head of a linked list in pythonlinked list python implementationpython linked listshow to make linked list on pythonlinked list python example codelinked list in python using listhow to traverse a linked list in pythonlinked list implementation inpythonlinked list pythonhow to values of a linked are in another linked list pythonbasic linkedlist operations in pythonlinked list in python step by steppython linked lists linksingle linked list pythonlinked list in python programinsert into linked list pythonlinked list python are calledlinked list python tutoriallistnode in pythonpython linked list data structure classlinked list 3d linked list pythonlinkedin urls list lython linked list python3linked lis in pythonginitialize linked list node in pythonlinke list in pythonwhen to use linked list in pythonuses of linkedlist in pythonlinked list pytohn 5dsimple linked list in pythonlinklist in pythonbest way to implement a linked list pythoncreate python linkedlistlinked list python programwhat is list node in pythonhow to call linked list in pythonlinked list python 2chow to do a linked list in pythonlinked list program in python whole codelinked list pythinimplement linked list pythonlinked list python methodslist and listnode pythonpython inbuilt linkedlistpython 3 linked listlinked lisy pythonlinked list with pythonpython linked list to listsigly linked list in pythonhow to save elements of a linked list to a queue in pythonlinked lists python codelinked list implementation pythonlinked list python exampleare linked lists needed in pythoncreate linked list by pythonpython learn linkedlistpython built in linked listimplement linked list in pythonare there built in methods for linked lists in pythonlinked list in python inbuiltpython lists are linked listslist de link pythonone way linked list pythonimport linked list pythoninsert between linked list pythonpython list linked listhow to create linked list in pythonlearn how to implement linked list in pythonlinked lists are written in pythonlinked list python with methodslinked list python explainedlinked lsit pythontraverse linked list pythonlinkelist in pythondefine a loinked list in python linked lists in pythonlinked list package in pythonhow to use linked lists in pythonpython linkedlist implementationuse of linked list in pythonpython linked list builtinpython example linkedlisthow to link lists together pythonare python lists linked listspython linked list codepython linked linklnked list defautl data structrei n pythonsingly linked list implementation in pythonsingly linked list in pythonlinkedlist from linkedlist pythonlinked list pythonlinked list in pythoinhow to create a node in linked list pythondisplay linked list in pythonlist in python is singly linkedlinked list implementation using pythonis linked list in pythonlinkedin list pythonlinkedin list in pythonpythonn linked listpython create a simple linked listlinked chain implementation in pythonpython linked list implementationpython linkedlist builthow to form the linkedlist in pythonpython list or linked listwhat is a linked list pythonhow to define a linked list in pythonlinked list functions python implement linked list pythoncreate a linked list in python with methodsmake a linked list in pythondata structures and algorithms linked list pythonis list in python a linked listlinked lists python tutorialpython linked lists 27how to create a linked list pythonlink list python examplelinkedlist implementtation pythonpython list linked list 3fpython create linked listpython source code linked listlinked list using list in pythonpython linked list tutorialpython how to inmplement linked listimport linked list pythobnlinkedin python librarypython for in linked listpython linked list classhow to build linked list with a list pythonpython implement a linked list codesingly linked list python examplehow to access the elements of a linked list in pythoncan you import a linked list in pythondata structure linked list in pythonlist node to list pythonlinked list operations pythonhow linked list used for in programming in phythonpython linked list standard librarylinled linsertions python algorithmsingle linked list in pythonusing python to define a linked listlinked list python structurepython linkedlist classmaking a linked list in pythonqueue linked list pythonwhy do i do python linked listlist node in pythondo python list act as linked listwhat is linked list called in pythonwhat are linked list pyhtonimplement linkedlist pythonpython linked list exampleswhat are linked lists in pythonis there linked list in pythonaccesing linked list pythonwhat are the built in linked lists in python 3flinklist pythonpython implementation of linked listpython linked list documentationworking with listnodes in pythoncreate a linkedlist pythonhow to create a linked list in python 3linkedlist in python3program to demonstrate linked list in pythonhow to make the linked list in pythonlinked llist in pythondoubly linked list in pythonadding elements of a linkedlist pythonmake a linked list pythonset linked list pointer pytholinked list in pytonsingly linked operations in data structure code pythonlist using linked list in pythonlinked list put pythonlinked list pythonlinked lists pythonhow to code a linked list in pythonpython example of a linked listexample of using of linked lists pythonlinked list python modulelinked list python 3linked list in python with explanationcreate linked list from list pythonpython code linked listpython return linked listhow to display pointers in a linked list in pythonis python list a linked listpython how to create a linked listlist nodes in pythonlinked list in tkinterpython linked list how topython linked code pythonlinked list method in pythonpython linked classread linked list pythonlinkedlist using pythonimplementing linked list in pythonsingly linked list pythonwhat is a linked list in pythonhow to implement a linked list in pythonlinked list project in pythonpy linked listlinked list implementation of pythonpython library linkedlistmost common singly linked list class in pythonis a python list a linked listusing linked list in pythonlinked lists python3 tutorialsyntax of create linked list in pythonlinked list in python useswhat is linked list in pythonlinked list in python libraryhow to make linked lists pythoncreate linked list code pythonpython implement linked listadding to a linked list pythonpython code linkedlistpython how to make a linked listpython adding t linked listslinked list object pythonimplement a linked list in pythonbimplementing linked lists in pythonlinked list python usesadvantages of linked lists in pythonhow to use linked list in pythonunderstanding linked lists in pythonmethods in linked list pythonlinked list in python codelinked list inpythondefine a linked list in pythonlinkd list pythonhow to implement linked list in pythonlinked list insertions python algorithmhow to go back to beginigng of linkedlist python in python linked listlinked list types in pythonpython3 linked listhow to make linked list object pythonusing link in linked list in pythonpython linked list traverse a linked list pythonlinked list program in pythoncreate a linked list pythonhow to use builtin linked list in pythonhow to declare linked list in pythonstore a number in linked list pythonhow to create a linked list in pythonlinked list methods in pythonpython program for linked listlinked list code pythonlinked list operationsin pythonpython doubly linked listpython list nodelinked list concept in pythonlinked list class code pythonpython linked list insertionhowto linked list in python python class linked listlinked list in python tutorialunderstanding linked lists pythonmake linked list in python using one class onlyhow to create linked list pythonbound to a list python what ispython 3 linked listslinked list all operations in pythoncreate a linked list in pythonhow to make linked list in pythoncreation of linked list in pythonare linked lists used in pythonlinked list iin pythonpython how to do linked listcreate a linked list in python examplelinked list python guidepython link listpython making linked listsimplementation of linked list in pythonsingly linked list using pythonsingle linked list python examplebuild a linked list pythonlista implementation in pythondoes python have a built in linkedlistimplementation linked list in pythonlinked list sing pythonlinkedlist function in pythonis python list based on linked listlinked list library pythoncreating linked list in pythonlinked list python