From 6c85ce002d069299f068891d09f256e8cfd47006 Mon Sep 17 00:00:00 2001 From: Deven <63876261+devenperez@users.noreply.github.com> Date: Sun, 28 May 2023 18:58:16 -0400 Subject: [PATCH] Time: 0 ms (100.00%), Space: 43.7 MB (7.03%) - LeetHub --- ...83-remove-duplicates-from-sorted-list.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 83-remove-duplicates-from-sorted-list/83-remove-duplicates-from-sorted-list.java diff --git a/83-remove-duplicates-from-sorted-list/83-remove-duplicates-from-sorted-list.java b/83-remove-duplicates-from-sorted-list/83-remove-duplicates-from-sorted-list.java new file mode 100644 index 0000000..126ecf7 --- /dev/null +++ b/83-remove-duplicates-from-sorted-list/83-remove-duplicates-from-sorted-list.java @@ -0,0 +1,26 @@ +/** + * 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 current = head; + while (current != null) { + ListNode next = current.next; + + while (next != null && current.val == next.val) { + next = next.next; + } + + current.next = next; + current = next; + } + return head; + } +} \ No newline at end of file