-
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
69 lines (53 loc) · 1.42 KB
/
237. Delete Node in a Linked List.py
File metadata and controls
69 lines (53 loc) · 1.42 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
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
# -*- coding: utf-8 -*-
# @Time : 2019/3/4 15:26
# @Author : xulzee
# @Email : xulzee@163.com
# @File : 237. Delete Node in a Linked List.py
# @Software: PyCharm
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
node.val = node.next.val
node.next = node.next.next
def stringToListNode(nums):
# Generate list from the input
numbers = nums
# Now convert that list into linked list
dummyRoot = ListNode(0)
ptr = dummyRoot
for number in numbers:
ptr.next = ListNode(number)
ptr = ptr.next
ptr = dummyRoot.next
return ptr
def listNodeToString(node):
if not node:
return "[]"
result = ""
while node:
result += str(node.val) + ", "
node = node.next
return "[" + result[:-2] + "]"
def main():
try:
line = [1, 2, 3]
node = stringToListNode(line)
n = 1
ret = Solution().deleteNode(node, n)
out = listNodeToString(node)
if ret is not None:
print("Do not return anything, modify node in-place instead.")
else:
print(out)
except StopIteration:
pass
if __name__ == '__main__':
main()