-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_nodes_from_linked_list.py
More file actions
30 lines (30 loc) · 1.02 KB
/
Copy pathdelete_nodes_from_linked_list.py
File metadata and controls
30 lines (30 loc) · 1.02 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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def modifiedList(self, nums: List[int], head: Optional[ListNode]) -> Optional[ListNode]:
nums = set(nums)
while head.val in nums:
head = head.next
t = head
pre = None
while t:
if t.val in nums and t.next != None:
temp = t
while temp.next != None and temp.next.val in nums:
temp = temp.next
if temp.next == None and pre != None:
pre.next = None
return head
if temp.next == None and pre == None:
return []
t.val = temp.next.val
t.next = temp.next.next
elif t.val in nums and t.next == None:
if pre.next != None:
pre.next = None
pre = t
t = t.next
return head