LeetCode83. 删除排序链表中的重复元素2018_1205_1128

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

测试用例[1,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) {
        if(!head) return nullptr;
        ListNode* first=head;
        ListNode* second=head;
        while(second){
            if(first->val==second->val){
                second=second->next;
            }else{
                first->next=second;
                first=first->next;
            }
        }
        first->next=second;
        return head;
        
    }
};

猜你喜欢

转载自blog.csdn.net/qq_40200779/article/details/84825577
今日推荐