【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 ...
随机推荐
- .net4.0注册到IIS
IIS和.netfw4.0安装顺序是从前到后,如果不小心颠倒了,无所谓. 打开程序-运行-cmd:输入一下命令重新注册IIS C:\WINDOWS\Microsoft.NET\Framework\v4 ...
- TCP/UDP详解
转载:http://www.cnblogs.com/visily/archive/2013/03/15/2961190.html, 作者:望梅止渴 相关: HTTP协议详解 深入理解HTTP协议 T ...
- 搭建高性能计算环境(六)、应用软件的安装之lammps
1,上传需要的软件包lammps-stable.tar.gz. 2,解压缩并进入安装目录 tar xvf lammps-stable.tar.gz cd lammps-30Oct14 3,如果需要re ...
- 合并多个List<T>类型并通过LINQ按指定属性排序
后台CS代码: namespace WebFormTest.TestCollect { public partial class ListTest : System.Web.UI.Page { pro ...
- centos下安装php环境
centos下安装php环境 安装apache yum install httpd-devel 启动apache /etc/init.d/httpd start 安装mysql yum install ...
- 向Windows 日志管理器写入系统程序日志信息
标准样例代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Da ...
- 一键清理Windows垃圾的BAT文件代码
@echo off color 0atitle win7一键清理系统垃圾echo ★☆ ★☆ ★☆ ★☆ ★☆★☆★☆ ★☆ ★☆ ★☆ ★☆★echo ★☆ ★☆ ★☆ ★☆ ★☆★☆★☆ ★☆ ★ ...
- STM32F4_TIM输出PWM波形(可调频率、占空比)
Ⅰ.概述 上一篇文章关于STM32基本的计数原理明白之后,该文章是在其基础上进行拓展,讲述关于STM32比较输出的功能,以输出PWM波形为实例来讲述. 提供实例工程中比较实用的函数:只需要调用该函数, ...
- UART,USART,SPI,I2C等总线的介绍与区别20160526
首先来说一下UART和USART的区别: 1.字面意义: UART:universal asynchronous receiver and transmitter通用异步收发器: USART:univ ...
- javascript arguments
此文为转载文章: 什么是arguments arguments 是是JavaScript里的一个内置对象,它很古怪,也经常被人所忽视,但实际上是很重要的.所有主要的js函数库都利用了arguments ...