LeetCode83——Remove Duplicates from Sorted List

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.

难度系数:

容易

实现

ListNode *deleteDuplicates(ListNode *head) {
    ListNode *next = NULL;
    ListNode *curr = head;
    while (curr) {
        if (curr->next == NULL) {
            return head;
        }
        next = curr->next;
        if (curr->val == next->val) {
            curr->next = next->next;
            continue;
        }
        curr = curr->next;
    }
    return head;
}
时间: 2024-10-16 05:30:23

LeetCode83——Remove Duplicates from Sorted List的相关文章

Leetcode-83 Remove Duplicates from Sorted List

#83. Remove Duplicates from Sorted List 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. /** * Definition fo

LeetCode83 Remove Duplicates from Sorted List

题目: Given a sorted linked list, delete all duplicates such that each element appear only once. (Easy) For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 分析: 链表去重.还是链表题的主要特点就是考察代码实现,除了个别题,没太多算法. 代码: 1 /**

Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn'

Leetcode | Remove Duplicates from Sorted Array I && II

Remove Duplicates from Sorted Array I Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memor

26. Remove Duplicates from Sorted Array【easy】

26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with consta

26. Remove Duplicates from Sorted Array【leetcode】,数组,array,java,算法

26. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant mem

LeetCode(82): Remove Duplicates from Sorted List II

Remove Duplicates from Sorted List II:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->

LintCode - Remove Duplicates from Sorted List

LintCode - Remove Duplicates from Sorted List LintCode - Remove Duplicates from Sorted List Web Link Description Code - C Tips Web Link http://www.lintcode.com/en/problem/remove-duplicates-from-sorted-list/ Description Given a sorted linked list, del

leetCode 26.Remove Duplicates from Sorted Array(删除数组重复点) 解题思路和方法

Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory.