-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinklist.py
More file actions
83 lines (64 loc) · 1.51 KB
/
Linklist.py
File metadata and controls
83 lines (64 loc) · 1.51 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# define linklist
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class linklist:
def __init__(self):
self.head = None
def create(vals):
link = linklist()
if not vals: return link
link.head = ListNode(vals[0], None)
tail = link.head
for i in range(1, len(vals)):
node = ListNode(vals[i], None)
tail.next = node
tail = tail.next
return link
def length(link):
len = 0
node = link.head
while node:
len += 1
node = node.next
return len
def find(val, link):
cur = link.head
while cur:
if val == cur.val:
return cur
cur = cur.next
return None
def insertFront(val, link):
node = ListNode(val)
node.next = link.head
link.head = node
def insertRear(val, link):
cur = link.head
while cur.next:
cur = cur.next
cur.next = ListNode(val, None)
return link.head
def insertInside(link, index, val):
count = 0
cur = link.head
while cur and count < index - 1:
count += 1
cur = cur.next
if not cur:
return 'Error'
node = ListNode(val)
node.next = cur.next
cur.next = node
def delete(link, val):
head = link.head
while head and head.val == val:
head = head.next
link.head = head
while head and head.next:
if head.next.val == val:
head.next = head.next.next
else:
head = head.next
return link.head