题目

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

分析

如题所示,要求将给定链表的前半部分和后半部分交叉链接。

方法一:(解决问题但是TLE)

采用的方法是将前面的节点逐个与当前尾节点链接,当然,每次都需要求取当前尾节点,这就造成了平方的复杂度。

方法二:

首先,可以将给定链表一分为二,然后合并。

AC代码

class Solution {
public:
//方法一,逐个交换
void reorderList1(ListNode* head) {
if (!head || !head->next)
return ; //逐个节点元素值交换
ListNode *p = head, *pre = head , *q = head;
while (p)
{
//只剩下一个尾节点
if (q->next == NULL)
return; //寻找当前末尾节点
while (q->next->next)
{
q = q->next;
}
//保存末尾节点的前一个节点
pre = q;
//得到末尾节点
q = q->next; //处理完毕
if (p == pre)
return; //改变链接
q->next = p->next;
p->next = q;
//新末尾节点后继置空
pre->next = NULL; p = q->next;
q = p;
} return;
} void reorderList(ListNode* head) {
//空链表或单节点或双节点链表直接返回
if (!head || !head->next || !head->next->next)
return;
/* 先用快慢指针找到链表的中点,然后翻转链表后半部分,再和前半部分组合。
* 需要注意的是把链表分成两半时,前半段的尾节点要置为NULL,翻转链表时也要把尾节点置为NULL。
*/
ListNode *slow = head, *fast = head;
//把整个链表划分成2个等长的子链表,如果原链表长度为奇数,那么第一个子链表的长度多1
while (fast->next != NULL) {
fast = fast->next;
if (fast->next != NULL)
fast = fast->next;
else
break;
slow = slow->next;
}
ListNode *f_head = head, *s_head = slow->next;
//将前半部分链表尾节点链接到空
slow->next = NULL; //翻转第二个链表
ListNode *p = s_head, *q = s_head->next;
p->next = NULL;
while (q)
{
ListNode *r = q->next;
q->next = p;
p = q;
q = r;
}
s_head = p; //合并两个链表
p = f_head, q = s_head;
while (q)
{
//保存两个子链表下一节点
ListNode *f_r = p->next , *s_r = q->next;
p->next = q;
q->next = f_r; p = f_r;
q = s_r;
}//while
}
};

GitHub测试程序源码

LeetCode(143) Reorder List的更多相关文章

  1. 新概念英语(1-43)Hurry up!

    新概念英语(1-43)Hurry up! How do you know Sam doesn't make the tea very often? A:Can you make the tea, Sa ...

  2. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  3. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  4. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  5. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  6. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  7. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  8. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  9. LeetCode(4)Median of Two Sorted Arrays

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

随机推荐

  1. (转)使用参数SQL_SLAVE_SKIP_COUNTER处理mysql slave同步错误讨论

    使用参数SQL_SLAVE_SKIP_COUNTER处理mysql slave同步错误讨论 本文链接地址:http://blog.chinaunix.net/uid-31396856-id-57532 ...

  2. undefined is not a function

    具体报错 TypeError: c:\Users\Administrator\WebstormProjects\blogtest\views\index.ejs:1 >> 1| <% ...

  3. ruby 正则表达式 匹配中文

    1.puts /[一-龥]+/.match("this is 中文")                 =>中文 2.str2="123中文"puts / ...

  4. springboot+shiro+cas实现单点登录之shiro端搭建

    github:https://github.com/peterowang/shiro-cas 本文如有配置问题,请查看之前的springboot集成shiro的文章 1.配置ehcache缓存,在re ...

  5. ABAP数据类型

    数据类型表: 类型缩写 类型 默认长度 允许长度 初始值 描述 C 文本型 1   Space 字符串数据,如'Program' D 日期型 8 8 '00000000' 日期数据,格式为YYYYMM ...

  6. volley框架下发送和读取cookie

    首先volley本身不支持cookie,但是volley又非常好用(比如封装了网络请求的实现,内部支持并发, 不用我们再额外设计网络管理异步处理,网络请求不应在UI线程等等),那既想使用volley又 ...

  7. nmap -sT

    将与目标端口进行三次握手,尝试建立连接,如果连接成功,则端口开放,慢,且会被目录主机记录

  8. 洛谷 P2323 [HNOI2006]公路修建问题

    题目描述 输入输出格式 输入格式: 在实际评测时,将只会有m-1行公路 输出格式: 输入输出样例 输入样例#1: 4 2 5 1 2 6 5 1 3 3 1 2 3 9 4 2 4 6 1 3 4 4 ...

  9. MovieReview—Black Panther(黑豹)

    Justice & Evil   The night before the night, i saw the latest movie in the Marvel series at JiaH ...

  10. thinkphp 跳转

    1   $this -> redirect('index',array('type'=>2,'id'=>0));   //直接跳转 2  $this->success('提交失 ...