-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path237_delete_node_in_a_linked_list.py
More file actions
44 lines (41 loc) · 1.3 KB
/
Copy path237_delete_node_in_a_linked_list.py
File metadata and controls
44 lines (41 loc) · 1.3 KB
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
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
from linkedlist import listnodes, nodestolist
class Solution(object):
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
prev = node
while node.next is not None:
prev = node
node.val = node.next.val
node = node.next
prev.val = node.val
prev.next = node = None
vectors = [
[1,2,3], 2, [1,3],
[1,2,3,4,5], 2, [1,3,4,5],
[1,2,3,4,5], 3, [1,2,4,5],
[1,2,3,4,5], 4, [1,2,3,5]
]
for i in range(0, len(vectors), 3):
a = vectors[i]
v = vectors[i+1]
print(f'{a}, v={v}')
expected = vectors[i+2]
head = listnodes(a)
node = tail = head
while node.next is not None:
if node.val == v:
break
node = node.next
assert node != head, f'bad test: node must not point to the head!'
assert node.next is not None, f'bad test: node must not point to the tail!'
Solution().deleteNode(node)
returned = nodestolist(head)
assert expected == returned, f'for {a} and node {v} expected {expected}, but returned {returned}!'