【leetcode】Remove Duplicates from Sorted List (easy)

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.

思路:

简单题,没什么好说的。

class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        if(head == NULL)
            return NULL;

        ListNode * ans = head;
        ListNode * anscur = ans; //记录答案链表的最后一个指针
        ListNode * cur = head->next; //记录判断的是原链表中的哪一个指针
        while(cur != NULL)
        {
            if(anscur->val != cur->val) //只有值变化的时候才把指针接到ans链表后面 节省操作
            {
                anscur->next = cur;
                anscur = anscur->next;
            }
            cur = cur->next;
        }
        anscur->next = NULL; //防止最后的指针是重复的,给ans结尾
        return ans;
    }
};

大神的代码更加简洁, 没有必要新建一个头结点,就用原来的头结点就好。

class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
    if(NULL == head) return head;

    ListNode *cur = head, *nxt = head->next;
    while (nxt) {
        if (cur->val != nxt->val)
            cur = cur->next = nxt;
        else if (!nxt->next)
            cur->next = nxt->next;
        nxt = nxt->next;
    }

    return head;
}
};
时间: 2024-10-09 18:39:27

【leetcode】Remove Duplicates from Sorted List (easy)的相关文章

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

【leetcode】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 me

leetcode 之Remove Duplicates from Sorted Array(二)

描述    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] 之前的想法是再加一个计数的变量就行了 int removeDeplicates1(

【leetcode】 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. 给出一个已排序链表,删除所有重复的元素使每一个节点值只出现一次 思路: 1. 定义pCurNode,pNextNode两个

【LeetCode】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】Remove Duplicates from Sorted List in JAVA

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. 思路很简单,由于乖乖的sort好了,就是判断下一个是不是比它大就好了,如果大,那么跳过下一个直接link到下一个的下一

【leetcode】Remove Duplicates from Sorted List II (middle)

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->1->1->2->3, return 2->3.

【Leetcode】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->1->1->2->3, return 2->3.

【Leetcode】Remove Duplicates from Sorted Array II

题目:对上一题的延伸,每个数字可以出去2次. 思路:还是设置两个下标.第一个lenxb标记已去重的地方,第二个i标记待处理的位置.每次比较时,比较lenxb和lenxb-1两个位置,如果都相等,说明出现超过两次了:否则满足要求. 注意:通过上面的思路可知,特判情况是长度小于等于2时. PS:提交后16ms,在此题的提交时间第一队列最前位置.Yes! 代码: class Solution { public: int removeDuplicates(vector<int>& nums)