1 Star 0 Fork 0

唐梓迅/leetcode题解

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
.LeetCode 143 1.08 KB
一键复制 编辑 原始数据 按行查看 历史
唐梓迅 提交于 2022-04-29 09:00 . 重排链表
struct ListNode* midList(struct ListNode* head)
{
struct ListNode* slow = head;
struct ListNode* fast = head;
while (fast->next != NULL && fast->next->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
void mergeList(struct ListNode* l1,struct ListNode* l2)
{
struct ListNode* temp1;
struct ListNode* temp2;
while (l2 != NULL) {
temp1 = l1->next;
temp2 = l2->next;
l1->next = l2;
l1 = temp1;
l2->next = l1;
l2 = temp2;
}
}
struct ListNode* reserveList(struct ListNode* head)
{
struct ListNode* pre = NULL;
struct ListNode* cur = head;
while (cur != NULL) {
struct ListNode* nextTemp = cur->next;
cur->next = pre;
pre = cur;
cur = nextTemp;
}
return pre;
}
void reorderList(struct ListNode* head){
struct ListNode* mid = midList(head);
struct ListNode* L1 = head;
struct ListNode* L2 = mid->next;
mid->next = NULL;
L2 = reserveList(L2);
mergeList(L1,L2);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Tang-CMer/leetcode-problem-solving.git
git@gitee.com:Tang-CMer/leetcode-problem-solving.git
Tang-CMer
leetcode-problem-solving
leetcode题解
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385