forked from SamirPaulb/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09.py
70 lines (46 loc) · 1.27 KB
/
09.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
class Node:
def __init__(self,data):
self.data = data
self.ref = None
class LinkedList:
def __init__(self):
self.head = None
def print_LL(self):
if self.head is None:
print("The Linked List is Empty")
else:
n = self.head
while n is not None:
print(n.data)
n = n.ref
def add_begin(self,data):
if self.head is None:
self.head = Node(data)
else:
new_node = Node(data)
new_node.ref = self.head
self.head = new_node
def add_end(self,data):
if self.head is None:
self.head = Node(data)
else:
new_node = self.head
while new_node.ref is not None:
new_node = new_node.ref
new_node.ref = Node(data)
def add_after(self,data,n):
tmp = self.head
i = 0
while i < n and tmp is not None:
i = i+1
tmp = tmp.ref
newNode = Node(data)
newNode.ref = tmp.ref
tmp.ref = newNode
LL1 = LinkedList()
LL1.add_begin(10)
LL1.add_end(2000)
LL1.add_begin(20)
LL1.add_end(300)
LL1.add_after(99,2)
LL1.print_LL()