forked from SamirPaulb/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13_ Delete At the Beginning.py
68 lines (56 loc) · 1.51 KB
/
13_ Delete At the Beginning.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
class Node:
def __init__(self,data):
self.data = data
self.next = 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.next
def add_begin(self,data):
if self.head is None:
self.head = Node(data)
else:
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def add_end(self,data):
if self.head is None:
self.head = Node(data)
else:
n = self.head
while n.next is not None:
n = n.next
n.next = Node(data)
def after_node(self,data,x):
if self.head is None and x != 0:
print("Number is out of bound")
else:
n = self.head
i =0
while i <x and n is not None:
i = i +1
n = n.next
new_node = Node(data)
new_node.next = n.next
n.next = new_node
def delete_begin(self):
if self.head is None:
print("The Linked list is Empty")
else:
self.head = self.head.next
LL = LinkedList()
LL.add_begin(10)
LL.add_begin(20)
LL.add_end(300)
LL.add_end(293)
LL.after_node(899,1)
LL.after_node(10033,4)
LL.delete_begin()
LL.print_LL()