-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonLists.py
More file actions
69 lines (58 loc) · 1.69 KB
/
Copy pathpythonLists.py
File metadata and controls
69 lines (58 loc) · 1.69 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 -*-
"""
Created on Wed Nov 27 15:23:29 2019
@author: 11104510
python实现单向链表
"""
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
def get_data(self):
return self.val
def set_next(self, node):
self.next = node
class SingleLinkedLists:
def __init__(self, head = None):
self.head = head
def append(self, data):
node = ListNode(data)
if self.head is None:
self.head = node
return node
curr_node = self.head
while curr_node.next is not None:
curr_node = curr_node.next
curr_node.next = node
return node
def find(self, data):
curr_node = self.head
while curr_node.next is not None and curr_node.val != data:
curr_node = curr_node.next
return curr_node
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 print(self):
output = []
curr_node = self.head
while curr_node.next is not None:
output.append(curr_node.val)
curr_node = curr_node.next
output.append(curr_node.val)
print(output)
if __name__ == '__main__':
input_list = [1,3,5,4]
input_delete_data = 5
sll = SingleLinkedLists()
for data in input_list:
sll.append(data)
sll.print()
delete_node = sll.find(input_delete_data)
sll.deleteNode(delete_node)
sll.print()