Skip to content

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

Open
@Animenzzzz

Description

@Animenzzzz

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions