Leetcode 83. Remove Duplicates from Sorted List 移除重复 链表版 解题报告

1 解题思想

昨天的那道题反而是2,今天的反而是1,leetcode的编排很奇怪

Leetcode 82. Remove Duplicates from Sorted List II 移除重复 链表版-2

直接删除就好了,不多说,太简单。

上一题是遇到重复的不增加,这道题是只增加一个,一样的

2 原题

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.

Subscribe to see which companies asked this question

3 AC解

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * }直接删除头就好 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode p=head;
        while(p!=null){
            while(p.next!=null && p.val==p.next.val){
                p.next=p.next.next;
            }
            p=p.next;
        }
        return head;
    }
}

你可能感兴趣的:(Leetcode 83. Remove Duplicates from Sorted List 移除重复 链表版 解题报告)