Leetcode#143 Reorder List
先把链表分割成前后两半,然后交叉融合
实践证明,凡是链表相关的题目,都应该当成工程类题目做,局部变量、功能函数什么的随便整,代码长了没关系,关键是清楚,不容易出错。
代码:
ListNode *reverseList(ListNode *head) {
if (!head) return head;
ListNode *prev = head;
head = head->next;
prev->next = NULL;
while (head) {
ListNode *next = head->next;
head->next = prev;
prev = head;
head = next;
}
return prev;
}
ListNode *mergeList(ListNode *head1, ListNode *head2) {
ListNode *next1, *next2;
ListNode *head = head1;
while (head1 && head2) {
next1 = head1->next;
next2 = head2->next;
head1->next = head2;
if (next1)
head2->next = next1;
head1 = next1;
head2 = next2;
}
return head;
}
void reorderList(ListNode *head) {
if (!head)
return;
ListNode *fast = head;
ListNode *slow = head;
while (fast && fast->next) {
fast = fast->next->next;
slow = slow->next;
}
ListNode *head1 = head;
ListNode *head2 = reverseList(slow->next);
slow->next = NULL;
mergeList(head1, head2);
}
Leetcode#143 Reorder List的更多相关文章
- leetcode 143. Reorder List 、86. Partition List
143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...
- Java for LeetCode 143 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 ...
- leetcode 143. Reorder List ----- java
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 ...
- Leetcode 143. Reorder List(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 thi ...
- [leetcode]143. 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 may not mod ...
- 【LeetCode】143. Reorder List 解题报告(Python)
[LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 143. Reorder List - LeetCode
Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前 ...
- 【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 ...
- LeetCode OJ 143. 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 ...
随机推荐
- jquery 中如何将数组转化为json字符串,然后再转化回来?
其实可以这样: $.fn.stringify = function() { return JSON.stringify(this); } 然后这样调用: $(array).stringify(); 转 ...
- 数据库设计(字段)中的char、varchar、text和nchar、nvarchar、ntext的区别
char.varchar.text和nchar.nvarchar.ntext的区别 1.CHAR.CHAR存储定长数据很方便,CHAR字段上的索引效率级高,比如定义char(10),那么不论你存储的数 ...
- DataGrid1
2013-09-2512:26:31 1.DataGrid页面 <asp:DataGrid ID="msgInfo1" PageSize="8" Auto ...
- Excel中 设置使得每行的颜色不一样
在编写测试案例的时候,众多的excel行看的眼睛花花的,这里给出一个小技巧,设置Excel的每行显示的颜色不一样,最终的效果如下: 具体操作: 1. Ctrl+A全选所有表格区域 ...
- windows phone 8 开发系列(三)程序清单说明与配置
一 清单文件内容介绍 当我们先建了一个项目之后,我们可以看到vs自动会为我们创建了很多文件,正常人都会先一个个去翻看下每个文件都是干啥的,都主要写了些啥,在这些文件中,在Properies目录下面,我 ...
- jQuery 实现网页图片动态游走,碰到边框反弹
学学jQuery,实现个小功能练练手 需要用到定时器 html代码如下 <html> <head> <title></title> <script ...
- 使用FormData上传文件、图片
关于FormData XMLHttpRequest Level 2添加了一个新的接口 ---- FormData 利用FormData对象,可以通过js用一些键值对来模拟一系列表单控件,可以使用XM ...
- Discuz 3.X 门户文章插入图片自动添加 alt 标签
最近用 Discuz 搭建了个网站--儿童安全座椅网(www.bbseat.com.cn),用到了门户功能,不得不说Discuz 的功能还是非常强大的,但在使用过程中发现在发表文章时添加了图片却不能像 ...
- php 文件上传简单类---限制仅上传jpg文件
php 文件上传代码,限制只能上传jpg格式文件,也可以自行添加其它扩展名的文件. <?php /* * 图片上传类 仅限JPG格式图片 * edit by www.jbxue.com at 2 ...
- cookie工作原理
当客户访问某个基于PHP技术的网站时,在PHP中可以使用setcookie()函数生成一个cookie,系统经处理把这个cookie发送到客户端并保存在C:\Documents andSettings ...