Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LeetCode] 82. Remove Duplicates from Sorted List II #2

Open
Animenzzzz opened this issue Jul 13, 2019 · 0 comments
Open

[LeetCode] 82. Remove Duplicates from Sorted List II #2

Animenzzzz opened this issue Jul 13, 2019 · 0 comments

Comments

@Animenzzzz
Copy link
Owner

description

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

Example 1:

Input: 1->2->3->3->4->4->5
Output: 1->2->5

Example 2:

Input: 1->1->1->2->3
Output: 2->3

以前写的代码,太长时间没有整理看过了。。。回头重新整理issue

C解题:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* deleteDuplicates(struct ListNode* head) {
    struct ListNode* head_tmp = head;
    struct ListNode* result = NULL;
    struct ListNode* result_head = NULL;
    if(!head) return NULL;
    if (head->next == NULL) {
        return head;
    }
    while(head){
        //头结点开始重复,要剔除头结点
        if (head->val == head->next->val) {
            while(head->val == head->next->val){
                head = head->next;
                if (!head) {
                    return result_head;
                }
                if (!head->next) {
                    break;
                }
                //3,3,4,4这种情况
                if (head->val != head->next->val) {
                    if (head->next->next) {
                        if (head->next->val == head->next->next->val) {
                            head = head->next;
                        }
                    }
                }
            }
            head = head->next;
            if (!result) {
                if (!head) {
                    return NULL;
                }
                struct ListNode* tmp = (struct ListNode*)malloc(sizeof(struct ListNode));
                tmp->next = NULL;
                tmp->val = head->val;
                result = tmp;
                result_head = tmp;
            }else{
                if (head) {
                    struct ListNode* tmp = (struct ListNode*)malloc(sizeof(struct ListNode));
                    tmp->next = NULL;
                    tmp->val = head->val;
                    result->next = tmp;
                    result = result->next;
                }
            }
            if (head){
                head = head->next;
            }
        //头结点不重复
        }else{
            if (!result) {
                if (!head) {
                    return NULL;
                }
                struct ListNode* tmp = (struct ListNode*)malloc(sizeof(struct ListNode));
                tmp->next = NULL;
                tmp->val = head->val;
                result = tmp;
                result_head = tmp;
            }else{
                if (head) {
                    struct ListNode* tmp = (struct ListNode*)malloc(sizeof(struct ListNode));
                    tmp->next = NULL;
                    tmp->val = head->val;
                    result->next = tmp;
                    result = result->next;
                }
            }
            if (head){
                head = head->next == NULL? NULL:head->next;
            }
        }
        if (!head) {
            break;
        }
        if (head->next == NULL) {
            result->next = head;
            break;
        }
    }
    printf("");
    return result_head;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant