1 Star 0 Fork 0

wangzai/力扣算法面试题

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
143. Reorder List.cpp 1.40 KB
一键复制 编辑 原始数据 按行查看 历史
wangzai 提交于 2021-11-15 20:59 . 修改了 143 和 206
//
// Created by 旺崽 on 2021/10/31.
//
#include "bits/stdc++.h"
using namespace std;
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:
ListNode *middleNode(ListNode *head) {
ListNode *slow = head, *fast = head;
while (fast->next != nullptr) {
slow = slow->next;
fast = fast->next;
if (fast->next != nullptr) fast = fast->next;
}
return slow;
}
ListNode *reverseList(ListNode *head) {
ListNode *pre = nullptr;
ListNode *cur = head;
while (cur) {
ListNode *next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
void reorderList(ListNode *head) {
ListNode *start = head;
head = middleNode(head);
ListNode *tail = head->next;
head->next = nullptr;//封尾
head = start;
ListNode *ans = head;
tail = reverseList(tail);
while (tail != nullptr) {
ListNode *c = head->next;
ListNode *d = tail->next;
head->next = tail;
tail->next = c;
head = c;
tail = d;
}
head = ans;
}
};
int main() {
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qianlinyi/algorithmicQuestions.git
git@gitee.com:qianlinyi/algorithmicQuestions.git
qianlinyi
algorithmicQuestions
力扣算法面试题
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385