Share my Simple JAVA solution


  • 0
    W
    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if (head == null || head.next == null) return head;
            
            ListNode preHead = new ListNode(0);
            preHead.next = head;
            ListNode pre = head.val != head.next.val ? head : preHead;
            ListNode runner = head.next;
            int curVal = head.val;
            
            while (runner != null){
                if (runner.val != curVal){
                    curVal = runner.val;
                    if (runner.next == null || runner.val != runner.next.val){
                        pre.next = runner;
                        pre = pre.next;
                    }
                }
                runner = runner.next;
            }
            pre.next = runner;
            
            return preHead.next;
        }
    }

Log in to reply
 

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