Skip to content

Commit ab692d6

Browse files
committed
Create 0203-remove-linked-list-elements.kt
1 parent 8f4638b commit ab692d6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Example:
3+
* var li = ListNode(5)
4+
* var v = li.`val`
5+
* Definition for singly-linked list.
6+
* class ListNode(var `val`: Int) {
7+
* var next: ListNode? = null
8+
* }
9+
*/
10+
class Solution {
11+
fun removeElements(head: ListNode?, `val`: Int): ListNode? {
12+
13+
var dummy = ListNode(0, head)
14+
var current = head
15+
var prev = dummy
16+
17+
while(current != null) {
18+
if(current?.`val` == `val`)
19+
prev.next = current.next
20+
else
21+
prev = current
22+
current = current.next
23+
}
24+
25+
return dummy.next
26+
}
27+
}

0 commit comments

Comments
 (0)