Skip to content

Commit a9834b1

Browse files
committed
Updated
1 parent 0081d76 commit a9834b1

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

DSA/linked_list.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
class Node:
22
def __init__(self, data):
33
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")
517

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)
1419

15-
nodeA = Node(6)
16-
nodeB = Node(3)
17-
nodeC = Node(4)
18-
nodeD = Node(2)
19-
nodeE = Node(1)
2020

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+

0 commit comments

Comments
 (0)