原题地址

先把链表分割成前后两半,然后交叉融合

实践证明,凡是链表相关的题目,都应该当成工程类题目做,局部变量、功能函数什么的随便整,代码长了没关系,关键是清楚,不容易出错。

代码:

 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的更多相关文章

  1. leetcode 143. Reorder List 、86. Partition List

    143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...

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

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

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

  5. [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 ...

  6. 【LeetCode】143. Reorder List 解题报告(Python)

    [LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  7. 143. Reorder List - LeetCode

    Question 143. Reorder List Solution 题目大意:给一个链表,将这个列表分成前后两部分,后半部分反转,再将这两分链表的节点交替连接成一个新的链表 思路 :先将链表分成前 ...

  8. 【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 ...

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

随机推荐

  1. swift 闭包简写实际参数名$0、$1等理解

    Swift 自动对行内闭包提供简写实际参数名,你也可以通过 $0 , $1 , $2 等名字来引用闭包的实际参数值. 如果你在闭包表达式中使用这些简写实际参数名,那么你可以在闭包的实际参数列表中忽略对 ...

  2. iOS - 数组(NSArray)

    1. 数组的常用处理方式 //--------------------不可变数组 //1.数组的创建 NSString *s1 = @"zhangsan"; NSString *s ...

  3. iOS数据持久化(三)

    #pragma mark - Core Data stack /** * @synthesize 关联成员变量和属性 */ @synthesize managedObjectContext = _ma ...

  4. 【学习笔记】【C语言】选择结构-if

    1.if的第1种结构 if(条件) {     语句1;     语句2;     ...... } 如果if右边小括号()中的条件成立,也就是为“真”时,就会执行大括号{}中的语句: 如果条件为假, ...

  5. 20141124-HTML-JavaScrilpt

    JavaScript JavaScript(简称JS),他是一门HTML的脚本语言,用来改进设计.验证表单.检测浏览器.创建cookies,以及更多的应用. 他的用法及语法类似于Visual Stud ...

  6. 《JavaScript高级程序设计》心得笔记-----第二篇章

    第五章 9.Function函数 1)         函数内部有两个特殊的对象: (1)       arguments(主要用于保存函数参数,有一个属性callee,这是一个指针,指向拥有argu ...

  7. extern 数组

    最近比较关注C++对象的Linkage类型,然后今天突然想起extern数组这个奇葩的东西,稍微折腾了一下,顺手写个随笔. 首先在cpp中定义几个数组: ,,,,}; ,,,,}; ,,,,}; 然后 ...

  8. const关键字在C和C++区别

    1)C++默认为内部链接:C默认为外部链接2)在C++中,一般一个const不会创建内存空间,而是将其保存在符号表(待看).比如: ; char buf[bufsize]; 这里无需为const创建内 ...

  9. struts2中constant参数设置

    序号 方法 说明 1 <constant name="struts.i18n.encoding" value="UTF-8"/> 指定web应用默认 ...

  10. PCB参数计算神器-Saturn PCB Design Toolkit下载及安装指南

    进行PCB设计,特别是高频高速设计时,难免会涉及到PCB相关参数的计算及设置,如:VIA的过流能力,VIA的寄生电容.阻抗等,导线的载流能力,两相互耦合信号线间的串扰,波长等参数. 这里向大家介绍一款 ...