其他分享
首页 > 其他分享> > 【leetcode】143. 重排链表

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

 

标签:head,ListNode,struct,temp,arr,链表,leetcode,pst,143
来源: https://www.cnblogs.com/ganxiang/p/14173748.html