-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.py
163 lines (143 loc) · 4.56 KB
/
linked_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# single linked list
# take reference:https://www.youtube.com/watch?v=qp8u-frRAnU
# forst we'll create a class to create nodes
class Node:
def __init__(self,data=None, next=None):
self.data=data #assigning data to the data variable
self.next=next #initializing next node as null (None in python)
# creating class for linked list
class LinkedList:
def __init__(self):
self.head=None #initializing head pointer
def insert_at_beginning(self, data):
node=Node(data,self.head) #next will point to head of next node
self.head=node #head will point to new node which is inserted at beginning
def insert_at_end(self,data):
# if L_list is empty insert at beginning
if self.head is None:
self.head=Node(data, None)
return
itr=self.head
while itr.next:
itr=itr.next
#at the end of the while loop, itr.next will be pointing to None
# that means we reached the end of the linkedlist
# now we'll create a new node from our given data and will assign to the itr.next
# that means itr.next will point to our node
itr.next=Node(data, None)
def insert_values(self, data_list):
# here we'll be converting list in LinkedList
self.head=None #wiping out existing data
for data in data_list:
self.insert_at_end(data)
def get_len(self):
# we'l count the total no of heads present in the LL
count=0
itr=self.head
while itr:
count+=1
itr=itr.next
return count
def remove_at_index(self,index):
if index<0 or index>=self.get_len():
# raise Exception("Invalid Index")
print('Invalid Index')
return
if index==0:
self.head=self.head.next
return
itr=self.head
count=0
while itr:
if count==index-1:
itr.next=itr.next.next
break
itr=itr.next
count+=1
def insert_at_index(self, index, data):
if index<0 or index>=self.get_len():
# raise Exception("Invalid Index")
print('Invalid Index')
return
if index==0:
self.insert_at_beginning(data)
return
itr=self.head
count=0
while itr:
if count==index-1:
temp=itr.next
itr.next=Node(data, temp)
del temp
break
itr=itr.next
count+=1
def print_LL(self):
if self.head is None:
print("LinkedList is empty")
else:
itr=self.head
ll_str=""
while itr:
ll_str+=str(itr.data)+"-->"
itr=itr.next
print(ll_str)
if __name__=="__main__":
'''ll=LinkedList()
ll.insert_values(["abc","pqr","xyz","hex","dec"])
# ll.remove_at_index(-1)
ll.insert_at_index(2, "banana")
ll.print_LL()'''
ll=LinkedList()
ll.insert_values([7,5,2,63,15,98,12])
ll.remove_at_index(1)
ll.insert_at_index(2, 55)
ll.print_LL()
'''ll=LinkedList()
ll.insert_at_beginning(10)
ll.insert_at_end(20)
ll.insert_at_end(30)
ll.insert_at_end(40)
ll.insert_at_end(50)
ll.remove_at_index(2)
ll.insert_at_index(3, 156)
ll.print_LL()'''
'''
class Node:
def __init__(self, data=None, next= None):
self.data=data
self.next=next
class LinkedList():
def __init__(self):
self.head=None
def insert_at_end(self, data):
node=Node(data, None)
itr=self.head
# if list is empty node will get added at beginning
if itr is None:
self.head=node
else:
# if list is not empty we'll traverse till the end of the list
while itr:
if itr.next is None: #at the end of the Linkedlist, next points to None
#now we'll assign our node to next , bcz we're insering at end
itr.next=node
break
itr=itr.next
def print_LL(self):
itr= self.head
if itr is None:
print("LinkedList is empty")
return
else:
ll_str=""
while itr:
ll_str+=str(itr.data)+"-->"
itr=itr.next
print(ll_str)
ll=LinkedList()
ll.insert_at_end(10)
ll.insert_at_end(20)
ll.insert_at_end(30)
ll.print_LL()
'''