-
Notifications
You must be signed in to change notification settings - Fork 5
/
leetcode-203-remove-linked-list-elements.swift
145 lines (106 loc) · 3.8 KB
/
leetcode-203-remove-linked-list-elements.swift
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
Copyright (c) 2020 David Seek, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
https://leetcode.com/problems/remove-linked-list-elements/
Remove all elements from a linked list of integers that have value val.
Example:
Input: 1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
*/
/**
In place approach.
Faster -> Gets us into the 100$ range.
And doesn't use any space.
Big O Annotation
Time complexity O(n) where n is the amount of nodes in head.
Space complexity O(1) where removing in place / not using extra space.
*/
func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
// Return nil if we don't have a head node
guard let head = head else {
return nil
}
// Get a reference to the head
var current: ListNode? = head
// Check that the head doesn't need to be removed
while current != nil && current!.val == val {
// If so, set the next node as new head
current = current?.next
}
// Store a reference to the final head
var root: ListNode? = current
// While we still have nodes...
while current != nil {
// Check, that the next one exists and is not .val == val
while current!.next != nil && current!.next!.val == val {
/**
If so, cut the node out and set to next.next
Will be repeated until we don't have .val == val
*/
current!.next = current!.next!.next
}
/**
We need to make sure, that the final .next.val
is not .val for niche cases
*/
if current!.next?.val == val {
current!.next = nil
}
// Continue with the next .next
current = current?.next
}
return root
}
/**
Stored values approach.
Takes longer and is less space efficient.
Big O Annotation
Time complexity O(n) where n is the amount of nodes in head.
Space complexity O(n) where n is the amount of nodes in head.
*/
func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
// Get an array of values of the list
var values: [Int] = []
head?.traverse { values.append($0) }
// Filter by the desired removal value
values = values.filter { $0 != val }
// Init a new list
var newHead: ListNode? = ListNode(-1)
// Store it's head
var root: ListNode? = newHead
// Iterate through values
for value in values {
// And init a new node for every value
let node = ListNode(value)
// Set it
newHead?.next = node
// And store it as next
newHead = node
}
// Return .next to lose the -1 head
return root?.next
}
/**
Simple function to traverse
a list node by node
*/
extension ListNode {
func traverse(_ visit: (Int) -> Void) {
visit(val)
next?.traverse(visit)
}
}