-
Notifications
You must be signed in to change notification settings - Fork 0
/
task1.py
108 lines (96 loc) · 2.87 KB
/
task1.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
class Node:
def __init__(self, v):
self.value = v
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def add_in_tail(self, item):
if self.head is None:
self.head = item
else:
self.tail.next = item
self.tail = item
def print_all_nodes(self):
node = self.head
while node is not None:
print(node.value)
node = node.next
def find(self, val):
node = self.head
while node is not None:
if node.value == val:
return node
node = node.next
return None
def delete(self, val, all=False):
node = self.head
previous_node = None
while node is not None:
if node.value == val:
if previous_node:
previous_node.next = node.next
else:
if node.next:
self.head = node.next
else:
self.head = node.next
self.tail = node.next
node = node.next
if not all:
break
continue
previous_node = node
node = node.next
def clean(self):
# node = self.head
# previous_node = None
# while node is not None:
# if previous_node:
# del previous_node
# if not node.next:
# del node
self.head = None
self.tail = None
def find_all(self, val):
node = self.head
nodes = []
while node is not None:
if node.value == val:
nodes.append(node)
node = node.next
return nodes
def len(self):
node = self.head
length = 0
while node is not None:
length += 1
node = node.next
return length
def insert(self, afterNode, newNode):
if not afterNode:
newNode.next = self.head
self.head = newNode
node = self.head
while node is not None:
if node == afterNode:
newNode.next = node.next
node.next = newNode
node = node.next
def to_python_list(self):
node = self.head
nodes = []
while node is not None:
nodes.append(node.value)
node = node.next
return nodes
def sum_of_lists(firstList: LinkedList, secondList: LinkedList) -> LinkedList:
resList = LinkedList()
if firstList.len() == secondList.len():
list1 = firstList.to_python_list()
list2 = secondList.to_python_list()
sum_p_list = [sum(x) for x in zip(list1, list2)]
for val in sum_p_list:
resList.add_in_tail(Node(val))
return resList