Um momento
Leetcode / 83. Remove Duplicates from Sorted List

Pick a programming language:

Here is the source code for the solution to this problem.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode curr = head;

        while (curr != null) {
            if (curr.next != null && curr.next.val == curr.val) {
                ListNode next = curr.next;
                curr.next = curr.next.next;
                // Deallocate memory for duplicate node (might be garbage collected)
                next.next = null;
            }
            else {
                curr = curr.next;
            }
        }

        return head;
    }
}
Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: