LCR 026. 重排链表

https://leetcode.cn/problems/LGjMqU/

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    void reorderList(ListNode *head) {
        if (head == nullptr) { // 如果链表为空,直接返回
            return;
        }
        vector<ListNode *> vec; // 创建一个vector来存储链表中的节点
        ListNode *node = head; // 从头节点开始遍历链表
        while (node != nullptr) { // 当节点不为空时继续遍历
            vec.emplace_back(node); // 将当前节点添加到vector中
            node = node->next; // 移动到下一个节点
        }
        int i = 0, j = vec.size() - 1; // 初始化两个指针i和j,分别指向vector的头部和尾部
        while (i < j) { // 当i小于j时,继续循环
            vec[i]->next = vec[j]; // 将vec[i]的下一个节点设置为vec[j]
            i++; // i向右移动一位
            if (i == j) { // 如果i和j相等,跳出循环
                break;
            }
            vec[j]->next = vec[i]; // 将vec[j]的下一个节点设置为vec[i]
            j--; // j向左移动一位
        }
        vec[i]->next = nullptr; // 将最后一个节点的next指针设置为nullptr,表示链表结束
    }
};
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    // 408算法原题:先利用快慢指针找到中间节点,然后断开成俩部分,后半段完成翻转,然后与前半段进行合并
    void reorderList(ListNode* head) {
        if(!head||!head->next)return;
        // 1、利用快慢指针找到中间节点,slow为前半段
        ListNode *slow=head,*fast=head->next;
        while(fast&&fast->next){
            fast=fast->next->next;
            slow=slow->next;
        } 
        // 2、将后半段链表进行翻转
        ListNode *pre=nullptr,*cur=slow->next,*tmp;
        slow->next=nullptr;// 断开前半段链表
        // 开始翻转;翻转完成后pre为后半段的头节点
        while(cur){
            tmp=cur->next;
            cur->next=pre;
            pre=cur;
            cur=tmp;
        }
        // 3、将前半段与后半段进行合并
        cur=head;
        while(cur&&pre){
            //if(cur&&!pre)return;
            ListNode *tmp1=cur->next,*tmp2=pre->next;
            cur->next=pre;
            pre->next=tmp1;
            cur=tmp1,pre=tmp2;
        }
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值