Remove Duplicates from Sorted List

来源:互联网 发布:殊知的意思是什么 编辑:程序博客网 时间:2024/06/08 05:06
/**
 * 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 *tmp=head;
        if (head==NULL)
        return head;
        else if(head->next==NULL)
        return head;
        else{
            while(tmp->next!=NULL)
            {
                if(tmp->val==tmp->next->val)
                {
                   tmp->next=tmp->next->next;
                }
                else
                tmp=tmp->next;
            }
        return head;
        }
    }
};
0 0
原创粉丝点击