【Reorder List】cpp
题目:
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}.
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void reorderList(ListNode *head) {
if (!head) return;
ListNode dummy(-);
dummy.next = head;
ListNode *p1 = &dummy, *p2 = &dummy;
// get the mid Node
for (; p2 && p2->next; p1 = p1->next, p2 = p2->next->next);
// reverse the second half list
for ( ListNode *prev = p1, *curr = p1->next; curr && curr->next; ){
ListNode *tmp = curr->next;
curr->next = curr->next->next;
tmp->next = prev->next;
prev->next = tmp;
}
// cut the list from mid and merge two list
for ( p2 = p1->next, p1->next = NULL,p1 = head; p2; ){
ListNode *tmp = p1->next;
p1->next = p2;
p2 = p2->next;
p1->next->next = tmp;
p1 = tmp;
}
}
};
Tips:
改了两次,终于AC的效率进入5%了。
算法的思路:
Step1. 找到中间节点(p1指向中间节点之前的节点)
Step2. 把后半个List进行reverse操作
Step3. 讲两个Lists进行merge操作
尽量减少循环体中的无谓语句(例如条件判断语句、赋值语句、开新的中间变量),这样可以提升程序效率。
===========================================
第二次过这道题,考察三个内容
(1)双指针找链表中点
(2)翻转链表
(3)合并链表
合并链表的部分不太熟练,虽然自己也写出来了AC的代码,但是有些丑陋。复习了之前的代码,把这三部分都完善了一下。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void reorderList(ListNode* head) {
ListNode dummpy(-);
dummpy.next = head;
// find mid ListNode
ListNode* p1 = &dummpy;
ListNode* p2 = &dummpy;
while ( p2 && p2->next )
{
p1 = p1->next;
p2 = p2->next->next;
}
// reverse the second
ListNode* second = p1->next;
p1->next = NULL;
ListNode dummpy2(-);
dummpy2.next = second;
ListNode* curr = second;
while ( curr && curr->next )
{
ListNode* tmp = curr->next;
curr->next = tmp->next;
tmp->next = dummpy2.next;
dummpy2.next = tmp;
}
// merge two sub lists
p1 = dummpy.next;
p2 = dummpy2.next;
while ( p2 )
{
ListNode* tmp = p1->next;
p1->next = p2;
p2 = p2->next;
p1->next->next = tmp;
p1 = tmp;
}
}
};
【Reorder List】cpp的更多相关文章
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- leetcode 【 Reorder List 】python 实现
题目: 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 ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
- 【Path Sum】cpp
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
随机推荐
- IOS绘图——简单三角形
#import <UIKit/UIKit.h> @interface MyView : UIView @end #import "MyView.h" @implemen ...
- MongoDB用户管理
1. 创建一个超级用户 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 use admin db.createUser( { user: "adminUse ...
- MySQL下查看用户和建立用户
启动数据库: [root@server ~]# mysqld_safe & [1] 3289 [root@server ~]# 130913 08:19:58 mysqld_safe Logg ...
- phpStorm使用技巧及快捷键
下面是PhpStorm的注册码.Key,其license由用户名和License值组成. User name: EMBRACE License key: ===== LICENSE BEGIN === ...
- 谈谈对MVC的理解
MVC是Model-View-Controler的简称,即模型-视图-控制器.其实MVC是一种设计模式,它强制性的把应用程序的输入.处理和输出分开.MVC中的模型.视图.控制器它们分别承担着不同的任务 ...
- C/C++ 对常见字符串库函数的实现
在c中的string.h头文件中存在很多对字符串进行操作的函数,利用这些函数可以方便的对字符串进行操作.下面将对常见的字符串函数进行解释和实现. strcpy 函数原型:char* _strcpy(c ...
- flask-cors 实现跨域请求
安装:pip install -U flask-cors from flask import Flask from flask.ext.cors import CORS app = Flask(__n ...
- PHP 文件操作函数大全
<?php 读取文件夹: $handler = opendir("c:\");//打开文件夹 while($dir = readdir($handler)){//遍历文件夹 ...
- swift学习(二)--基本运算符、字符串、集合操作
在这一篇博客里面,我想要介绍一下swift里面一些常用的基本运算符,还有涉及到的字符串,集合操作.你会发现在swift里面还是有许多其他语言所不具有的特性运算操作的. 首先最基本的+,-,*,/,&g ...
- android开发图片分辨率问题解决方案
dpi是什么呢? dpi是“dot per inch”的缩写,每英寸像素数. 四种密度分类: ldpi (low), mdpi (medium), hdpi (high), and xhdpi (ex ...