forked from KDF5000/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteNode.py
More file actions
56 lines (43 loc) · 1.03 KB
/
Copy pathDeleteNode.py
File metadata and controls
56 lines (43 loc) · 1.03 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
# coding:utf-8
__author__ = 'devin'
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
p1 = node
p2 = node.next
while p2 is not None:
p1.val = p2.val
if p2.next is None:
p1.next = None
break
p1 = p2
p2 = p2.next
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
if __name__ == '__main__':
node = ListNode(1)
p = node
for i in range(2, 5):
temp = ListNode(i)
p.next = temp
p = p.next
p = node
while p is not None:
print p.val
p = p.next
s = Solution()
s.deleteNode(node)
p = node
while p is not None:
print p.val
p = p.next