1. 程式人生 > >LeetCode83. 刪除排序連結串列中的重複元素2018_1205_1128

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; } };