Elegant Java iterative solution


  • 0
    I
    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            ListNode first = head;
            while(first != null) {
                while(first.next != null && first.next.val == first.val) {
                    first.next = first.next.next;
                }
                first = first.next;
            }
            return head;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.