1
- // A pythom program for all operations performed on singly linked - list .
2
- // Time - Complexity = O (n )
3
- // Space - Complexity = O (n )
1
+ # A pythom program for all operations performed on singly linked-list.
2
+ # Time-Complexity = O(n)
3
+ # Space-Complexity = O(n)
4
4
class Node :
5
- def __init__ (self , data = None , next = None ): // Creation of Node
5
+ def __init__ (self , data = None , next = None ): # Creation of Node
6
6
self .data = data
7
7
self .next = next
8
8
9
9
class LinkedList :
10
10
def __init__ (self ):
11
- self .head = None // head points the first node
11
+ self .head = None # head points the first node
12
12
13
13
def print (self ):
14
14
if self .head is None :
15
15
print ("Linked list is empty" )
16
16
return
17
17
itr = self .head
18
- llstr = '' // empty string
18
+ llstr = '' # empty string
19
19
while itr :
20
20
llstr += str (itr .data )+ ' --> ' if itr .next else str (itr .data )
21
21
itr = itr .next
22
22
print (llstr )
23
23
24
- def length (self ): // will calculate length of the linked list
24
+ def length (self ): # will calculate length of the linked list
25
25
count = 0
26
26
itr = self .head
27
27
while itr :
@@ -31,7 +31,7 @@ def length(self): // will calculate length of the linked list
31
31
return count
32
32
33
33
def insert_at_begining (self , data ):
34
- node = Node (data , self .head ) // Creating a new node calling Node method
34
+ node = Node (data , self .head ) # Creating a new node calling Node method
35
35
self .head = node
36
36
37
37
def insert_at_end (self , data ):
@@ -77,7 +77,7 @@ def remove_at(self, index):
77
77
itr = self .head
78
78
while itr :
79
79
if count == index - 1 :
80
- itr .next = itr .next .next // to delete the specified node
80
+ itr .next = itr .next .next # to delete the specified node
81
81
break
82
82
83
83
itr = itr .next
0 commit comments