Kotlin Leetcode - 83. Remove Duplicates from Sorted List
class Solution {
fun deleteDuplicates(head: ListNode?): ListNode? {
}
}
解題思路
這題的輸入值是 Linked List
邏輯非常的單純
迴圈比對目前的值和下一個元素的值即可
Kotlin 參考解答
class Solution {
fun deleteDuplicates(head: ListNode?): ListNode? {
var cur = head
while (cur != null) {
while (cur.next != null && cur.`val` == cur.next.`val`) {
cur.next = cur.next.next
}
cur = cur.next
}
return head
}
}
回到 leetcode 列表