zoukankan      html  css  js  c++  java
  • [LeetCode#83]Remove Duplicates from Sorted List

    The problem:

    Given a sorted linked list, delete all duplicates such that each element appear only once.

    For example,
    Given 1->1->2, return 1->2.
    Given 1->1->2->3->3, return 1->2->3.

    My analysis:

    When sovling linkedlist problems, two boundary cases needed to be very carefully treated, otherwise it may result in head lossing or get null pointer exception. 
    1. the head node might be changed. 
    Thus we may lose the information for new result linked list. One classic way is to use a dummy node to always pointer the the head the list. 
    In this problem, when duplicates appear, we would keep one copy. Thus the head node could never be changed, we won't need to set a dummy head for this case.
    
    2. If we have a scaner over the linkedlist, or several scanner(which we only need to consider the faster scanner). 
    The usually checking condition when traversaling the list is "while (faster != null)", In this case, we could only guarantee at the moment of entering the while loop, the faster is not null pointer. However, we may move the faster in the while loop:
    like:
    while (faster != null && ptr1.val == faster.val) {
        faster = faster.next;
    }
    the faster may reach the end of the list, thus faster is null.
    if we have a statement: "fast = faster.next" without any checking, it would result in a null pointer exception. 
    
    ???if we add a checking over here, it seems to distort the logic !
    In fact, the boundary case is the ending procedure over the list, after it, the invariant is over, we would not need to care about that!!!

    My first solution:

           if (head == null)
                return null;
            ListNode ptr1 = head;
            ListNode ptr2 = head.next;
            
            while (ptr2 != null) {
                while (ptr2 != null && ptr1.val == ptr2.val) {
                    ptr2 = ptr2.next;
                }
                ptr1.next = ptr2;
                ptr1 = ptr2;
                if (ptr2 != null) //may directly reach the end
                    ptr2 = ptr2.next;
            }
            return head;

    My elegant solution:

    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if (head == null)
                return null;
            ListNode dummy = new ListNode(0);
            ListNode pre = dummy;
            ListNode cur = head;
            while (cur != null) {
                if (pre == dummy || pre.val != cur.val) {
                    pre.next = cur;
                    pre = cur;
                }
                cur = cur.next;
            }
            pre.next = null; //remember the last step!!!
            return dummy.next;
        }
    }
  • 相关阅读:
    相似矩阵
    特征值和特征向量
    非齐次方程组的通解
    线性方程组解的性质和结构、基础解系
    高斯消元法求齐次线性方程的通解
    从零开始学 Web 之 HTML(三)表单
    从零开始学Web之HTML(二)标签、超链接、特殊符号、列表、音乐、滚动、head等
    从零开始学 Web 之 HTML(一)认识前端
    JavaScript基础(一)概述
    如何恢复windows的exe文件的默认打开方式
  • 原文地址:https://www.cnblogs.com/airwindow/p/4255282.html
Copyright © 2011-2022 走看看