LeetCode(80)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.

分析

删除链表中重复元素结点。

该题目本质很简单,只需一次遍历。需要注意的是,要释放删除的结点空间。

AC代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if (head == NULL || head->next == NULL)
            return head;

        ListNode *p = head, *q = p->next;
        while (p && q)
        {
            if (p->val == q->val)
            {
                ListNode *t = q;
                p->next = q->next;
                q = q->next;
                //释放要删除的结点空间
                delete t;
            }
            else{
                p = p->next;
                q = q->next;
            }//elif
        }//while
        return head;
    }
};

GitHub测试程序源码

时间: 2024-10-09 10:05:17

LeetCode(80)Remove Duplicates from Sorted List的相关文章

LeetCode(2) 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 const

LeetCode(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 memory. For example, Given input array n

LeetCode记录之26——Remove Duplicates from Sorted Array

国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等.拿起键盘干就行了.然后返回有序项的下标就可以. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not

<LeetCode OJ> 83. Remove Duplicates from Sorted List

83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: Easy 题目意思:如今有一个已经排好顺序的链表,删除全部反复的节点.使每一个节点都仅仅出现一次! Given a sorted linked list, delete all duplicates such that each element appear only once. For exampl

LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II

1. 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. 这道题不难.具体程序

leetcode || 80、Remove Duplicates from Sorted Array II

problem: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. Hide Tags Array Two Pointers 题意:对数组进行去

LeetCode 80: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 83)Remove Duplicates from Sorted Lists

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. 题目要求: 给一有序链表,删除重复的结点,使得每个元素只出现一次. 解题思路: 1.从头到尾遍历链表,如果前后两个结点相同

leetcode第26题--Remove Duplicates from Sorted Array

problem: 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. For example,Given input ar