您的位置:首页 > 其它

LeetCode OJ Remove Duplicates from Sorted List II

2015-03-23 00:40 375 查看
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbers from the original list.

For example,

Given
1->2->3->3->4->4->5
, return
1->2->5
.

Given
1->1->1->2->3
, return
2->3
.

class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        ListNode *temp;
        ListNode *before_temp;
        ListNode *new_head = head;
        int to_delete_int;
        
        //the specil solution of the NULL input
        if (head == NULL) {
            return NULL;
        }
        
        //the specil solution of the duplicates in head:
        while (head->next != NULL && head->val == head->next->val) {
            to_delete_int = head->val;
            ListNode *to_delete = head;
            while (to_delete != NULL && to_delete_int == to_delete->val) {
                ListNode *before_to_delete = to_delete;
                to_delete = to_delete->next;
                delete before_to_delete;
            }
            if (to_delete == NULL) {
                return NULL;
            }
            head = to_delete;
        }
        new_head = head;
        temp = head;
        
        while (temp->next != NULL) {
            if (temp->val == temp->next->val) {
                to_delete_int = temp->val;
                ListNode *to_delete = temp;
                while (to_delete != NULL && to_delete_int == to_delete->val) {
                    ListNode *before_to_delete = to_delete;
                    to_delete = to_delete->next;
                    delete before_to_delete;
                }
                if (to_delete == NULL) {  // if delete all the tail of the input
                    before_temp->next = NULL;
                    return new_head;
                }
                temp = to_delete;
                before_temp->next = temp;
            } else {
                before_temp = temp;
                temp = temp->next;
            }
        }
        return new_head;
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: