[LeetCode]Remove Duplicates from Sorted List

来源:互联网 发布:模拟城市未来之城 mac 编辑:程序博客网 时间:2024/06/08 18:09
解题思路:
边界条件:head == NULL
前条件:front = head, next = front->next
不变式:front->val == next->val , 删除next指向的node; 否则,front->next, next = next->next

结束条件:next == NULL 

/** * 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 == NULL){            return NULL;        }        ListNode* front = head;        ListNode* next = front->next;        while(next != NULL){            if(front->val == next->val){                ListNode* temp = next;                front->next = temp->next;                next = front->next;                free(temp);            }else{                front = next;                next = next->next;            }        }        return head;    }};


0 0