linked list in python

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

showing results for - "linked list in python"
Julia
16 Aug 2016
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())
Marco
24 Feb 2016
1Full Linked List implementation at this link:
2https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/LinkedList
Olivia
28 Feb 2020
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
Mateo
02 May 2016
1class Node:
2    def __init__(self, data):
3        self.data = data
4        self.next = None
5
6    def __repr__(self):
7        return self.data
8
9class LinkedList:
10    def __init__(self):
11        self.head = None
12
13    def __repr__(self):
14        node = self.head
15        nodes = []
16        while node is not None:
17            nodes.append(node.data)
18            node = node.next
19        nodes.append("None")
20        return " -> ".join(nodes)
21
Greta
11 Oct 2018
1class LinkedList(object):
2    def __init__(self, head=None):
3        self.head = head
4
5	# ignored...  answer below
6    def get_position(self, position):
7        counter = 1
8        current = self.head
9        if position < 1:
10            return None
11        while current and counter <= position:
12            if counter == position:
13                return current
14            current = current.next
15            counter += 1
16        return None
17	# ignored...  answer above
181234567891011121314151617
queries leading to this page
linkedlist using pythonusing linked list in pythonwhen to use linked list over python listlinked list operation in pythonpython implement a linked list codeis there linked list in pythondoes python use linked listsis list in python a linked listlist and linked list in pythonlinked list example pythonlinked listpython3 linked listlnked list defautl data structrei n pythonhow to implement a linked list in pythonlinled linsertions python algorithmwhat does a linked list output look like in python for testcreation of linked list in pythonhow to values of a linked are in another linked list pythonlinked chain implementation in pythonhow to make linked list in pythonlinked list tutorial pythonhow to code a linked list in pythonlinked list library in pythonwhat is linked list in pythonpython how to create a linked listlearn linked lists pythonhow to work with linked lists in pythonfor linked list in pythonhow to make a linked lit in python with pointerspython code linked listcreate a linkedlist in pythonlinked lists in pythonlinked list python tutorialsingly linked operations in data structure code pythonhow to use linked lists pythonlinked list code pythonlinked list python example codelinked list all operations in pythonlinkd list pythonlist python linked listhow to create linked list pythonhow to use linked list in pythonlinkedlist pytho codelinkedlist python codelinked list pytholinked list python in detaillinked lists python 3linked list pthonpython tutor linked listlinked list i pythonwhat are linked lists 3f called in pythonlinkelist in pythonwhat are linked list pyhtonhow to create linked list in pythonhow to display pointers in a linked list in pythonlinked list code in pythonlist in python is singly linkedlinked list in python examplepython class linked listpythonlinked lists examplelinked list in python implementationpython for in linked listpython chained linked listlinked list definitionstore a number in linked list pythonpython return linked listwhat is an linked listpython linked lists tutorialpython create linked listlinked list pythinis a python list a linked listwhats a linked listdoes python have linked listslinked list inc implementing linked list in pythondoes python have a built in linked listlinked lists in python notesdata structures and algorithms linked list pythonlinklists in pythonpython list or linked listlinked list implementation using pythonlinked list pytonlinked list python librarylinked list using list in pythoncreate alinked list inb pyhtonpython linkeidn list implement linked list in pythonhow to define a linked list in pythonlinked list incpython linked list built inpython linked list examplehow to linked lists together pythonpython linked list how toare list in python linked listshow to create a linked list in python3linked lists with pythonlinked list python programis linked list in pythonwhat is a linked list python 3fimport linked list pythonhow to make linked list on pythonpythonn linked listusing python to define a linked listone way linked list pythonpython linked listdefinition of a function in python linked listpython lists are linked listswhen to use linked list in pythonin 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 linked list pythonlinked list python useshow to create linked list in pythonwhat is a linked list pythonlinkedlist function in pythonbuilt in linked list pythonlink and linked list in pyhtonqueue linked list pythonpython doubly linked listpython linked list methods linked listlinked list pythonlinked list implementation inpythonlinked list python linked listhow to fail linked list contains in pythontraverse linked list pythonlinked list on pythonlinked list 5b 5dwhat is a linked listare linked lists used in pythonhow to define a linked list pythondefine a loinked list in python how to use a linked list in pythonlinked list method in pythonsigly linked list in pythonare python lists linked listspython linked lsithow to do linked list 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 pythonhow to do linked lists work in pythonhow to define linked list in python linked list pythonpopulating an empty linked list in pythonpython code linkedlistlinked list python examplelinked list in python using listlistnode in pythonlinked list insertions python algorithmpython source code linked listlinked list in python getsingle linked list in pythonpython new linked listpython linkedlist builthow to make linked list object pythonlinked list python implementationlinkedin list pythonlinked 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 pythonhow do linked lists work pythonlinked list in python usespython linked list python linked list how to keep track of headread linked list pythonshould i do linked list in pythonpython how to do linked listlinked list in python3create linkedlist using list pythonlinkedlists pythonhow to traverse a linked list in pythonpython built in linked listhow to create a node in linked list pythonimplementing a linked list in pythonsingly linked list pythonimplement a linked list in pythonbpython insert into linked list return whole listsingle linked list python examplepython 3 linked listslinked list in pythonhow to create a linked list in pythonlinked list in pytonlinked list in python with explanationsingly linked list python examplebound to a list python what isdefine 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 listwhere is linked list usedpython 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 python linkedin librarylist using linked listhow to build linked list with a list pythonlist de link pythonlinked list in pthonunderstanding linked lists pythonuses of linkedlist in pythonlinked list python methodslinked list pythnolearn how to implement linked list in pythonpython create a simple linked listhow to make a linked list in pythonpython linkedlist librarylinked list in python tutorialpoinntwhat are linked lists in pythonpython program using linked listpython 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 pythonpython linked list head methodpython linked list functionslist nodes in pythonhow to import linked list in pythonlinked lists in python3linked list in python best explanationlinked list in pyimplementation of all the methods in linkedlist in pythonlinked list python explainedhow to work with linked lists pythonmake a linked list in pythonlinkedin python librarywhat does a linked list look like in pythonlinked list python structurepython linked lists examplelinked list python3linked list pythonpython linked listslinked list python codechained list pythonlinkedin list pythonhow create linked list in pythoncreate a linked list pythonhow to create a new linked list pythonlinked list pythonnhow to link lists together pythonpython linked list implementationinitilize linked list pythonpython linked listspython linked list sequencelinked lsts in pythonpython example linkedlisthow to make linked lists pythoncreate linked list by pythonlinked list pyrhonlinked list pythonlink list in pythonpython linkedinlistlinked 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 pythonget linked list python methodis python list a linked listlinked list pytohn 5dlinke list in pythontraverse a linked list pythonlinkedlist in pythonpython library linkedlistpython singly linked listlinked list sing pythonlinked list in pythoinare linked lists needed in pythonlinkear 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 pythonlinked list syntaxlinked list in python 3linked list traversal in pythonhow to return the head of a linked list in pythonlinked list in python libraryhow linked list used for in programming in phythonlink list pythonlinkedlist implementtation pythonpython implement linked listnext val linkied listhow to use builtin linked list in pythonlinked lists and nodes in pythonlinkedlist pythonpy linked listlinked lists python codepython library for linked listis python list based on linked listself in linkedlist pythonlinked lislist node pythonpython linked list builtinwhere are linked lists used pythonimplement linkedlist pythonimplement linked list in python includepython traverse a linked listlinked list in python program implement linked list pythonaccesing linked list pythonpython linked list containertraversing a linked list pythonusing linked lists pythonwhat are linked lists pythonwhat is linked listbasic linkedlist operations in pythonlinked list using pythonwhat is the linked list singly linked list using pythonlist to linked list pythonpython inbuilt linkedlistlinked list node class pythonlinkedlists in pythonsingly linked list pythinsingly linked list program in pythonlinked lisy pythonmake linked list in pythonlinked list implementation in pythonadvantages of linked lists in pythonwhat is linked list pythonpython linkedlistis python list linkedlinked list python guidedisplay linked list in pythondefine linked list in pythonlinked list pypython implement a linked list linked list python with methodslinklist pythonlinked llist in pythonpython linked list codelinkedinlist python codelinked llistlinked lsit in pythonlinked list with pythoncreate linked list from list pythonlinked list in python