【Lintcode】099.Reorder List
题目:
Given a singly linked list L: L0 → L1 → … → Ln-1 → Ln
reorder it to: L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …
Example
Given 1->2->3->4->null
, reorder it to 1->4->2->3->null
.
题解:
Spliting the list from the middle into two lists. One from head to middle, and the other from middle to the end. Then we reverse the second list. Finally we merge these two lists
Solution 1 ()
- class Solution {
- public:
- void reorderList(ListNode *head) {
- if (!head || !head->next) {
- return;
- }
- ListNode* mid = findMiddle(head);
- ListNode* right = reverse(mid->next);
- mid->next = nullptr;
- merge(head, right);
- }
- ListNode* findMiddle(ListNode* head) {
- ListNode* slow = head;
- ListNode* fast = head->next;
- while (fast && fast->next) {
- slow = slow->next;
- fast = fast->next->next;
- }
- return slow;
- }
- ListNode* reverse(ListNode* head) {
- if (!head || !head->next) {
- return head;
- }
- ListNode* pre = nullptr;
- while (head) {
- ListNode* tmp = head->next;
- head->next = pre;
- pre = head;
- head = tmp;
- }
- return pre;
- }
- void merge(ListNode* left, ListNode* right) {
- ListNode* dummy = new ListNode(-);
- int idx = ;
- while (left && right) {
- if (idx % == ) {
- dummy->next = left;
- left = left->next;
- } else {
- dummy->next = right;
- right = right->next;
- }
- dummy = dummy->next;
- ++idx;
- }
- if (left) {
- dummy->next = left;
- } else {
- dummy->next = right;
- }
- }
- };
from here
Solution 2 ()
- class Solution {
- public:
- /**
- * @param head: The first node of linked list.
- * @return: void
- */
- void reorderList(ListNode *head) {
- // write your code here
- if (head == NULL)
- return;
- vector<ListNode*> nodes;
- ListNode* iter = head;
- while(iter != NULL)
- {
- nodes.push_back(iter);
- iter = iter->next;
- }
- int LEN = nodes.size();
- int left = ;
- int right = LEN -;
- while(left < right)
- {
- nodes[left]->next = nodes[right];
- nodes[right--]->next = nodes[++left];
- }
- nodes[left]->next = NULL;
- }
- };
【Lintcode】099.Reorder List的更多相关文章
- 【LeetCode】143. Reorder List 解题报告(Python)
[LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【lintcode】 二分法总结 I
二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...
- 【Leetcode】143. Reorder List
Question: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You ...
- 【Lintcode】074.First Bad Version
题目: The code base version is an integer start from 1 to n. One day, someone committed a bad version ...
- 【Leetcode_easy】937. Reorder Log Files
problem 937. Reorder Log Files solution: class Solution { public: vector<string> reorderLogFil ...
- 【leetcode】937. Reorder Log Files
题目如下: You have an array of logs. Each log is a space delimited string of words. For each log, the f ...
- 【LeetCode】937. Reorder Log Files 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 分割和排序 日期 题目地址:https://leet ...
- 【LintCode】转换字符串到整数
问题描述: 实现atoi这个函数,将一个字符串转换为整数.如果没有合法的整数,返回0.如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN(-21 ...
- 【LintCode】判断一个字符串是否包含另一个字符串的所有字符
问题描述: 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母. 样例 给出 A = "ABCD" B = "ACD",返 ...
随机推荐
- URL Handle in Swift (一) -- URL 分解
更新时间: 2018-6-6 在程序开发过程之中, 我们总是希望模块化处理某一类相似的事情. 在 ezbuy 开发中, 我接触到了对于 URL 处理的优秀的代码, 学习.改进.记录下来.希望对你有所帮 ...
- POJ 2155 Matrix(二维树状数组,绝对具体)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20599 Accepted: 7673 Descripti ...
- rtems 4.11 RTC驱动 (arm, beagle)
RTC驱动的框架在 c/src/lib/libbsp/shared/tod.c 中,大部分功能都已经实现了,入口函数是 rtc_initialize(),BSP要实现的东西非常少. beagle的实现 ...
- oauth学习
https://www.cnblogs.com/blowing00/p/4524132.html
- 在diy的文件系统上创建文件的流程
[0]README 0.1) source code are from orange's implemention of a os , and for complete code , please v ...
- 【Scala】Scala的Predef对象
隐式引用(Implicit Import) Scala会自己主动为每一个程序加上几个隐式引用,就像Java程序会自己主动加上java.lang包一样. Scala中.下面三个包的内容会隐式引用到每一个 ...
- Yii的权限管理rbac
1.首先我们要在配置文件的组件(component)里面配置一下 Rbac 在对应项目下的config/main.php或者config/main-local.php下添加 'authManager' ...
- Firefox与chrome同步书签
1. 导出书签,保存为bookmarks.html 2. chrome导入即可
- 腾讯云上运行java程序过程
1: 购买服务器(腾讯云,阿里云等) 2:安装centos操作系统: 3:安装jdkhttp://www.cnblogs.com/Amos-Turing/p/7403696.html 4:安装数据库( ...
- 破解powerdesigner教程
点Tool