-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListNode.py
More file actions
74 lines (61 loc) · 1.78 KB
/
ListNode.py
File metadata and controls
74 lines (61 loc) · 1.78 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
from typing import List
class ListNode:
def __init__(self, val=0, next=None) -> None:
self.val = val
self.next = next
class LinkList:
def __init__(self) -> None:
self.head = None
def add(self, *node):
if not node:
print("Please input node val")
while node:
return None
def removeElements(self, head: ListNode, val: int) -> ListNode:
if not head: return None
dmp = cur = ListNode(0)
cur.next = head
while cur.next:
if cur.next.val == val:
cur.next = cur.next.next
else:
cur = cur.next
return dmp.next
def deleteDuplicates(self, head: ListNode) -> ListNode:
"""
delete duplicate node in order linked list
"""
cur = ListNode(0)
cur.next = head
if not head: return None
while head.next:
if head.val == head.next.val:
head.next = head.next.next
else:
head = head.next
return cur.next
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
"""
19 leetcode
remove nth node from end
"""
if not head: return None
dump = ListNode(0)
dump.next = head
node_num = 0
while head:
node_num += 1
head = head.next
times = node_num - n
j = 0
if times ==0:
dump.next == dump.next.next
else:
while head:
j += 1
if j == times:
head.next = head.next.next
break
else:
head = head.next
return dump.next