Dashboard Temp Share Shortlinks Frames API

HTMLify

Leetcode - Remove Duplicates from Sorted List - Dart
Views: 331 | Author: abh
 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
/**
 * Definition for singly-linked list.
 * class ListNode {
 *   int val;
 *   ListNode? next;
 *   ListNode([this.val = 0, this.next]);
 * }
 */
class Solution {
    ListNode? deleteDuplicates(ListNode? head) {
        if (head==null) {
            return head;
        }
        var th = head;
        while (th.next!=null) {
            if (th.val == th.next?.val) {
                th.next = th.next?.next;
                continue;
            }
            if (th.next != null) {
                th = th.next!;
            } else {
                th.next = null;
            }
        }
        return head;
    }
}