1. Reorder List My Submissions QuestionEditorial Solution

    Total Accepted: 64392 Total Submissions: 281830 Difficulty: Medium

    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}.

思路:

1.找到中点,前半部分最后一个点

a.找到后半部分第一个点

b.后半部分入栈

c.遍历前半部分,间隔性依次链接栈中节点

时间复杂度:

空间复杂度:

/**
* 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) {
int len=0;
if(head==NULL||head->next==NULL)return;
if(head->next->next==NULL)return;
ListNode *p=head,*fir_end,*sec_beg;
while(p){
len++;
p = p->next;
}
int mid=(len%2==0)?len/2:len/2+1;
mid = mid -1;
fir_end = head;
while(mid--){
fir_end=fir_end->next;
}
sec_beg = fir_end->next;
stack<ListNode*> slist;
while(sec_beg!=NULL){
slist.push(sec_beg);
sec_beg = sec_beg->next;
}
while(!slist.empty()){ //如果栈中有元素未被链接起来
ListNode *tmp=head->next,*stop=slist.top(); //保存前半部分当前节点下一个节点
head->next = stop; //链接栈中元素
stop->next = tmp; //栈中元素链接到原来当前节点的下一元素,相当于在中间插入
slist.pop();
head = tmp;
}
if(head!=NULL)head->next = NULL;//如果链长为奇数,最后一个元素指向空
}
};

66-Reorder List的更多相关文章

  1. 62. 链表重排[Reorder List]

    [本文链接] http://www.cnblogs.com/hellogiser/p/reorder-list.html [题目] Given a singly linked list L: L0→L ...

  2. reorder list(链表重新排序)

    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 thi ...

  3. [LeetCode] Reorder List 链表重排序

    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 th ...

  4. 配置OpenCV产生flann\logger.h(66): error C4996: 'fopen': This function or variable may be unsafe问题[zz]

    使用vs2012/2013配置opencv编译出现问题: 1>------ 已启动生成: 项目: Win32ForOpenCV245, 配置: Debug Win32 ------ 1> ...

  5. 【leetcode】Reorder List (middle)

    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 thi ...

  6. 13. Reorder List

    Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… Y ...

  7. String reorder

    本问题出自:微软2014实习生及秋令营技术类职位在线测试 (Microsoft Online Test for Core Technical Positions) Description For th ...

  8. Reorder List

    题目: 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 ...

  9. Scala 深入浅出实战经典 第66讲:Scala并发编程实战初体验

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  10. 重新想象 Windows 8 Store Apps (66) - 后台任务: 下载和上传

    [源码下载] 重新想象 Windows 8 Store Apps (66) - 后台任务: 下载和上传 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台任务 后台 ...

随机推荐

  1. [技术博客]WEB实现划词右键操作

    [技术博客]WEB实现划词右键操作 一.功能解释 简单地对题目中描述的功能进行解释:在浏览器中,通过拖动鼠标选中一个词(或一段文字),右键弹出菜单,且菜单为自定义菜单,而非浏览器本身的菜单.类似的功能 ...

  2. Linux入门需要搞清楚的思路问题

    很多同学接触linux不多,对linux平台的开发更是一无所知. 而现在的趋势越来越表明,作为一个优秀的软件开发人员,或计算机it行业从业人员,="" 掌握linux是一种很重要的 ...

  3. 华为HCIP-Eth-trunk原理知识点

    Eth-trunk(端口聚合.链路捆绑.链路聚合.以太通道) Eth-trunk技术出现的原因: • 随着网络中部署的业务量不断增长,对于全双工点对点链路,单条物理链路的带宽已不能满足正常的业务流量 ...

  4. shell 匿名管道和命名管道

    管道的特点:如果管道中没有数据,那么取管道数据的操作就会滞留,直到管道内进入数据,然后读出后才会终止这一操作:同理,写入管道的操作如果没有读取管道的操作,这一动作也会滞留. 1,匿名管道 匿名管道使用 ...

  5. Bzoj通过5题纪念

    我A了五题啦!!!

  6. Harbor仓库搭建及使用

    目录 一.docker配置 二.安装docker-compose 三.安装harbor 四.管理harbor 五.springboot项目配置docker 六.linux服务器上打包并推送至harbo ...

  7. js和jq文档操作

    JS文档操作 一.dom树结构 1.元素节点 2.文本节点 3.属性节点      不属于元素节点的子节点  4.文档节点(document) 二.处理元素节点    method    1.docu ...

  8. Device /dev/sdb excluded by a filter

    原因是添加的磁盘是在另一个虚拟机中新建的,已经有了分区表,现在的虚拟机并不能识别磁盘的分区表,运行parted命令重做分区表,中途需要输入三次命令(mklabel msdos -> yes-&g ...

  9. pvcreate vgcreate lvcreate 扩容

    centos6 服务器磁盘扩容 1.创建物理卷 /dev/sdb #pvcreate /dev/sdb 参数:/dev/sdb 设备名 2.创建卷组 vg_02 #vgcreate  vg_02  / ...

  10. Part 11 to 20 Basic in C# continue

    Part 11-12 switch statement in C# switch statement break statement   if break statement is used insi ...