算法练习LinkedList(六): P83

Remove Duplicates from Sorted List Easy

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

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3

直接的实现对于最后一个节点的处理不够精细,调试后完成了算法。

Runtime: 2 ms, faster than 6.29% of Java online submissions for Remove Duplicates from Sorted List.
Memory Usage: 36.9 MB, less than 100.00% of Java online submissions for Remove Duplicates from Sorted List.

public ListNode deleteDuplicates(ListNode head) {

    if(head == null || head.next == null) return head;

    List<Integer> unique = new ArrayList<>();

    ListNode dummy = new ListNode(0);
    dummy.next = head;

    ListNode t = dummy;
    while (head != null) {
        int v = head.val;
        if(!unique.contains(v)) {
            unique.add(v);
            t.next = head;
            t = t.next;
            head = head.next;
        }else {
            head = head.next;
            if(head == null) {
                t.next = null; //最后一个节点断开
                break;
            }
        }
    }
    return dummy.next;
}

还是题目意图理解的有偏差,Sorted List意味着链表中不可能有重复(重复2)这种结构的节点1->2->2->4->2。这种情况下,可以不依赖外部数据结构,直接操作链表即可——

public ListNode deleteDuplicates(ListNode n) {
    ListNode head = n;
    while (n != null) {
        if(n.next == null) {
            break;
        }
        if(n.val == n.next.val) {
            n.next = n.next.next;
        }else {
            n = n.next;
        }
    }
    return head;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容