LeetCode83. 刪除排序連結串列中的重複元素2018_1205_1128
阿新 • • 發佈:2018-12-31
/**
* 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;
}
};