File tree Expand file tree Collapse file tree 1 file changed +16
-21
lines changed Expand file tree Collapse file tree 1 file changed +16
-21
lines changed Original file line number Diff line number Diff line change 1
1
class Node :
2
2
def __init__ (self , data ):
3
3
self .data = data
4
- self .next = None # make None as the default value for next.
4
+ self .next = None
5
+
6
+ def insertBeginning (head , data ):
7
+ new_node = Node (data )
8
+ new_node .next = head
9
+ return new_node
10
+
11
+ def main ():
12
+ head = None
13
+ head = insertBeginning (head , "Shawn" )
14
+ head = insertBeginning (head , "Sean" )
15
+ head = insertBeginning (head , "John" )
16
+ head = insertBeginning (head , "Zyra" )
5
17
6
- def count_nodes (head ):
7
- # assuming that head != None
8
- count = 1
9
- current = head
10
- while current .next is not None :
11
- current = current .next
12
- count += 1
13
- return count
18
+ print (head .next .next .data )
14
19
15
- nodeA = Node (6 )
16
- nodeB = Node (3 )
17
- nodeC = Node (4 )
18
- nodeD = Node (2 )
19
- nodeE = Node (1 )
20
20
21
- nodeA .next = nodeB
22
- nodeB .next = nodeC
23
- nodeC .next = nodeD
24
- nodeD .next = nodeE
25
-
26
- print ("This linked list's length is: (should print 5)" )
27
- print (count_nodes (nodeA ))
21
+ main ()
22
+
You can’t perform that action at this time.
0 commit comments