码迷,mamicode.com
首页 > 编程语言 > 详细

leetcode 83. 删除排序链表中的重复元素(Remove Duplicates from Sorted List)

时间:2019-03-19 10:44:11      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:一个   重复   while   dup   str   pre   ted   题目   输出   

题目描述:

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

示例 1:

    输入: 1->1->2
    输出: 1->2

示例 2:

    输入: 1->1->2->3->3
    输出: 1->2->3

解法:


/**
 * 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;
        }else{
            ListNode* cur = head, *  nxt = head->next;
            while(nxt != NULL){
                while(nxt != NULL && nxt->val == cur->val){
                    nxt = nxt->next;
                }
                cur->next = nxt;
                cur = cur->next;
            }
            return head;
        }
    }
};

leetcode 83. 删除排序链表中的重复元素(Remove Duplicates from Sorted List)

标签:一个   重复   while   dup   str   pre   ted   题目   输出   

原文地址:https://www.cnblogs.com/zhanzq/p/10556786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!