其他分享
首页 > 其他分享> > LeetCode 0083 Remove Duplicates from Sorted List

LeetCode 0083 Remove Duplicates from Sorted List

作者:互联网

原题传送门

1. 题目描述

2. Solution 1

1、思路分析
迭代法。
2、代码实现

package Q0099.Q0083RemoveDuplicatesfromSortedList;

import DataStructure.ListNode;

public class Solution2 {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == cur.val)
                cur.next = cur.next.next;
            else cur = cur.next;
        }
        return head;
    }
}

3、复杂度分析
时间复杂度: O(n)
空间复杂度: O(1)

3. Solution2

1、思路分析
迭代法。
2、代码实现

package Q0099.Q0083RemoveDuplicatesfromSortedList;

import DataStructure.ListNode;

public class Solution1 {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        head.next = deleteDuplicates(head.next);
        return head.val == head.next.val ? head.next : head;
    }
}

3、复杂度分析
时间复杂度: O(n)
空间复杂度: O(1)

标签:head,ListNode,cur,Duplicates,复杂度,List,Remove,next,null
来源: https://www.cnblogs.com/junstat/p/16197463.html