【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.

解答:

很简单的模拟,因为是 sorted,所以找到不重复的下一个元素即可。

/**
 * 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)
            return head;

        if(head->next == NULL)
            return head;

        ListNode* tmp = head;
        ListNode* dup = NULL;
        while(tmp != NULL)
        {
            if(tmp->next != NULL && tmp->val == tmp->next->val){
                dup = tmp;
                while(dup->next != NULL && dup->next->val == dup->val)  dup = dup->next;
                dup = dup->next;

                tmp->next = dup;
            }
            tmp = tmp->next;
        }
        return head;
    }
};
时间: 2024-09-30 16:24:40

【LeetCode从零单刷】Remove Duplicates from Sorted List的相关文章

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(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

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

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【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. 简单的题目,直接上AC代码: ListNode* deleteDuplicates(ListNode* head) { i

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代码

【leetcode?python】83. Remove Duplicates from Sorted List

#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = None class Solution(object):    def deleteDuplicates(self, head):        if head==None or head

leetcode第83题-Remove Duplicates from Sorted List

这道题与实现数组中的删除重复元素类似.我们来看一下具体的过程,首先要判断是否为空(在这个上面吃了大苦头),删除下一个相同的元素的时候要定义一个tmp元素,再free掉即可. #include<stdio.h> #include<stdlib.h> struct ListNode { int val; ListNode *next; }; ListNode *deleteDuplicates(ListNode *head) { if (head) { struct ListNode