Skip to content

83. remove-duplicates-from-sorted-list

难度:Easy

刷题内容

原题连接

  • https://leetcode.com/problems/remove-duplicates-from-sorted-list/
  • 内容描述

Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2
Output: 1->2
Example 2:

Input: 1->1->2->3->3
Output: 1->2->3

���ⷽ��

˼· **- ʱ�临�Ӷ�: O(N)*- �ռ临�Ӷ�: O(1)***

��Ŀ�и内容描述����Ѿ�����õģ���ô����ֻ��Ҫ内容描述������һ���ڵ�������ڵ�ǰ�ڵ�ģ���ɾȥ

/**
 * 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) {
        ListNode* l = head;
        ListNode* l1 = head;
        if(l != nullptr)
        {
                while(l ->next != nullptr)
            {
                if(l ->val == (l ->next ->val))
                    l ->next = l ->next ->next;
                else
                    l = l ->next;
            }
        }
           return l1;
    }
};


回到顶部