小工具知识网

当前位置: 首页 > 行业知识 > 重排链表(leetcode)

行业知识

重排链表(leetcode)

2026-10-31 22:10:57 来源:互联网转载

struct ListNode* get_midlistnode( struct ListNode* head )

()

{

    struct ListNode* arr = NULL;

()

    struct ListNode* fast = head->next;

    struct ListNode* slow = head;

    while( fast != NULL && fast->next != NULL )

    {

        fast = fast->next->next;

        slow = slow->next;

    }

    arr = slow->next;

    slow->next = NULL;

    return arr;

}

找中间链点

struct ListNode* test( struct ListNode* head )

{

    struct ListNode* pos = (struct ListNode*)malloc(sizeof(struct ListNode));

    pos->next = head;

    struct ListNode* p1 = head;

    struct ListNode* p2 = head->next;

    while( p2 != NULL )

    {

        p1->next = p2->next;

        p2->next = pos->next;

        pos->next = p2;

        p2 = p1->next;

    }

    return pos->next;

}

将中间链点至最后反转

struct ListNode* test1(struct ListNode* headA , struct ListNode* headB )

{

    struct ListNode* pos = (struct ListNode*)malloc(sizeof(struct ListNode));

    struct ListNode* pf = pos;

    while( headA && headB )

    {

        pf->next = headA;

        headA = headA->next;

        pf = pf->next;

        pf->next = headB;

        headB = headB->next;

        pf = pf->next;

    }

    while( headA )

    {

    pf->next = headA;

    headA = headA->next;

    }

    return pos->next;

}

合并链表

void reorderList(struct ListNode* head){

    struct ListNode* mid_ListNode = NULL;

    if( head == NULL || head->next == NULL || head->next->next == NULL )

    {

        ;

    }

    else

    {

    mid_ListNode = get_midlistnode(head);

    mid_ListNode = test(mid_ListNode);

    head = test1(head,mid_ListNode);

    }

}

上一篇:Loading class `com.mysql.jdbc.Driver‘. This is deprecated.解决方法

下一篇:HTML标签之文字滚动效果(跑马灯效果),字节跳动前端岗位面试分享