欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Leetcode 83. Remove Duplicates from Sorted List

程序员文章站 2022-07-14 08:38:04
...

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Leetcode 83. Remove Duplicates from Sorted List

2. Solution

/**
 * 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* current = head;
        while(current) {
            if(current->next && current->val == current->next->val) {
                current->next = current->next->next;
                continue;
            }
            else {
                current = current->next;
            }
        }
        return head;
    }
};

Reference

  1. https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/
相关标签: Leetcode