【leetcode】143. 重排链表

 

void reorderList(struct ListNode* head){
    struct ListNode* arr[40000];
    struct ListNode* temp=(struct ListNode*)calloc(1,sizeof(struct ListNode));
    int pst=0;
    while(head){
        arr[pst++]=head;
        head=head->next;
    }
    for (int i=0; i<(pst+1)/2; i++){
        arr[i]->next=arr[pst-1-i];
        temp->next=arr[i];
        temp=arr[pst-1-i];
    }
    temp->next=NULL;
    return head;
}
void reorderList(struct ListNode* head){
    struct ListNode* arr[40000];
    struct ListNode* temp=(struct ListNode*)calloc(1,sizeof(struct ListNode));
    int pst=0;
    while(head){
        arr[pst++]=head;
        head=head->next;
    }
    for (int i=0; i<(pst+1)/2; i++){
        arr[i]->next=arr[pst-1-i];
        temp->next=arr[i];
        temp=arr[pst-1-i];
    }
    temp->next=NULL;
    return head;
}

 

posted @ 2020-12-22 16:28  温暖了寂寞  阅读(80)  评论(0编辑  收藏  举报