Reorder List
来源:互联网 发布:招商银行外汇牌价软件 编辑:程序博客网 时间:2024/05/16 10:54
题目链接:
https://leetcode.com/problems/reorder-list/description/
描述
Given a singly linked list L: L0?L1?…?Ln-1?Ln,
reorder it to: L0?Ln?L1?Ln-1?L2?Ln-2?…
You must do this in-place without altering the nodes’ values.
输入
For example,
Given {1,2,3,4},
输出
reorder it to {1,4,2,3}.
样例输入
样例输出
算法思想:
先把表等分成a与b两个链表,然后把b逆序,最后把a和b链表交叉合并起来即可
源代码
class Solution {public: void reorderList(ListNode* head) { if (!head) return ; ListNode *a, *b; //分离链表分成一半一半 a = head; b = head->next; while (b) { b = b->next; if (b) { a = a->next; b = b->next; } } b = a->next; a->next = NULL; a = head; //让b链表逆序 if (b) { ListNode *current = b->next; ListNode *tem = current; b->next = NULL; while (current) { current = current->next; tem->next = b; b = tem; tem = current; } } //合并2个链表 ListNode *a1, *a2, *b1; a1 = a; a2 = a1->next; b1 = b; while (b) { b = b->next; a1->next = b1; b1->next = a2; b1 = b; a1 = a2; if (a1) a2 = a1->next; } }};
最优源代码
算法复杂度:
阅读全文
0 0
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- Reorder List
- input框输入数字的问题
- 解析 Promise 原理,实现一个Promise
- Nmap 必备小技巧
- 一个比较不错的开源博客,可以参考一下,自己搭建
- Hadoop权威指南笔记二
- Reorder List
- 阿里云PCDN新亮点 自动调用HTTPDNS 解决域名劫持困扰
- 数据结构与算法(7)---Java语言实现:快速排序
- oppo手机权限开启方法3.1
- 技术焦虑症
- 【干货福利】67个拯救web开发者的工具、库和资源!——爱创课堂
- python 获取响应头
- oracle 常用语法
- cmc 是什么?