C实现 LeetCode->Remove Duplicates from Sorted List II(枚举法)(单链表)


Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers 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.

 上一题的加强版,要求删掉所有duplicates

 因为这种情况下,头结点有可能被删除,所以用了dummy节点。



C实现 LeetCode->Remove Duplicates from Sorted List II(枚举法)(单链表)_第1张图片

//
//  RemoveDuplicatesfromSortedListII.c
//  Algorithms
//
//  Created by TTc on 15/6/22.
//  Copyright (c) 2015年 TTc. All rights reserved.
//
/**
 *  Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers 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.
 
 上一题的加强版,要求删掉所有duplicates。
 因为这种情况下,头结点有可能被删除,所以用了dummy结点。
 */
#include "RemoveDuplicatesfromSortedListII.h"
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "List.h"


/********************************************************************/
// LeetCode 答案
/********************************************************************/
struct ListNode {
    int val;
    struct ListNode *next;
};
//单链表已经是排序好的
static struct ListNode*
deleteDuplicates(struct ListNode* head) {
    struct ListNode *curr = head;
    
    struct ListNode *dumbHead = (struct ListNode *)malloc(sizeof(*dumbHead));
    dumbHead->next = head;
    struct ListNode *dumbcurr = dumbHead;
    
    bool removeSelf = false;;
    
    while(curr != NULL && curr->next != NULL){
        //如果当前节点的值 等于 当前节点的下一个节点的值 则删除 当前节点指向的下一个节点
        if(curr->next->val == curr->val){
            curr->next = curr->next->next;
            removeSelf = true;//头节点 是重复的值 要背删除所有标记flag
        }else { //否则让当前节点指向下一个元素节点;准备下一次遍历条件
            if(removeSelf){ //如果要删除 头节点
                dumbcurr->next = curr->next;//将 要返回的 头节点的next指针指向 下一个节点
                removeSelf = false;
            }else dumbcurr = dumbcurr->next;
            curr = curr->next;
        }
    }
    
    if(removeSelf)  dumbcurr->next = curr->next;
    
    return dumbHead->next;
}
/********************************************************************/
/********************************************************************/


/********************************************************************/
// List.c 是范性类 单链表
/********************************************************************/
//单链表已经是排序好的
static ListElmt*
tt_deleteDuplicates(ListElmt* head) {
    
    ListElmt *curr = head;
    ListElmt *dumbHead = (ListElmt *)malloc(sizeof(*dumbHead));
    dumbHead->next = head;
    ListElmt *dumbcurr = dumbHead;
    
    bool removeSelf = false;;
    
    while(curr != NULL && curr->next != NULL){
        //如果当前节点的值 等于 当前节点的下一个节点的值 则删除 当前节点指向的下一个节点
        if((*(int *)(curr->next->data)) == (*(int *)(curr->data))){
            curr->next = curr->next->next;
            removeSelf = true;  //头节点 是重复的值 要背删除所有标记flag
        }else {
            if(removeSelf){
                dumbcurr->next = curr->next;
                removeSelf = false;
            }else dumbcurr = dumbcurr->next;
            curr = curr->next;
        }
    }
    if(removeSelf)  dumbcurr->next = curr->next;
    return dumbHead->next;
}

void
test_tt2_deleteDuplicates(){
    
    List l1;
    list_init(&l1, free);
    int *data ;
    int array[15] = {1,1,2,3,3,4,5,5,5,6}; //输入时排序好的链表
    for (int i = 0; i< 10; i++) {
        if ((data = (int *)malloc(sizeof(int))) == NULL)
            return ;
        *data = array[i];
        if (list_ins_next(&l1, NULL, data) != 0)//逐个插入元素
            return;
    }
    print_list(&l1);
    ListElmt *result = tt_deleteDuplicates(list_head(&l1));
    printf("result->val===%d\n",*(int *)result->data);
    print_listNode(result);
}


你可能感兴趣的:(C实现 LeetCode->Remove Duplicates from Sorted List II(枚举法)(单链表))