Easy
Given the head
of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
Example 1:
Input: head = [1,1,2]
Output: [1,2]
Example 2:
Input: head = [1,1,2,3,3]
Output: [1,2,3]
Constraints:
[0, 300]
.-100 <= Node.val <= 100
import com_github_leetcode.ListNode
/*
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
class Solution {
fun deleteDuplicates(head: ListNode?): ListNode? {
if (head == null) {
return null
}
var current: ListNode = head
var next = current.next
while (null != next) {
if (current.`val` == next.`val`) {
current.next = next.next
} else {
current = next
}
next = current.next
}
return head
}
}