Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→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}.

搞了一天多,纠结到一个while 写成了if...........

思路很清晰,  找出链表的中点,翻转第二个链表,合并两个链表。

b 站视频讲解

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* findMid(ListNode* head){ // 找出链表的中点,快慢指针解决,很经典。
/*if (head == NULL || head -> next == NULL){
return head;
}*/
ListNode* slow = head;
ListNode* fast = head;
while (fast != NULL && fast -> next != NULL){ // 注意判断条件很容易出问题
slow = slow -> next;
fast = fast -> next -> next;
}
return slow;
}
ListNode* reverseList(ListNode* head){ // 翻转一个链表
ListNode* newhead = NULL;
while (head != NULL){ // 一开始写成了if 纠结了一天,长记性。。。
ListNode* nextp = head -> next;
head -> next = newhead; newhead = head; // 统一后移
head = nextp;
}
return newhead;
}
ListNode* merge(ListNode* l1, ListNode* l2){ // 合并两个链表
if (l2 == NULL){
return l1;
}
ListNode* head = l1; while (l1 != NULL && l2 != NULL){
ListNode* nextp = l1 -> next;
l1 -> next = l2;
l2 = l2 -> next;
l1 -> next -> next = nextp; l1 = nextp; // l1后移
}
return head;
}
void reorderList(ListNode* head) {
if (head == NULL || head -> next == NULL){
return;
}
ListNode* mid = findMid(head); // 1.找到链表的中点
ListNode* l1 = head;
ListNode* l2 = mid -> next;
mid -> next = NULL; // 2. 断开链表
l2 = reverseList(l2); // 3. 翻转l2
head = merge(l1, l2); // 4. 合并两个链表
}
};

Leetcode 143. Reorder List(Medium)的更多相关文章

  1. LeetCode: 61. Rotate List(Medium)

    1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...

  2. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  3. LeetCode:11. ContainerWithWater(Medium)

    原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an  ...

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

  5. LeetCode: 62. Unique Paths(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...

  6. LeetCode: 56. Merge Intervals(Medium)

    1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...

  7. LeetCode: 55. Jump Game(Medium)

    1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...

  8. LeetCode: 54. Spiral Matrix(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...

  9. LeetCode:46. Permutations(Medium)

    1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...

随机推荐

  1. RHEL 5.7 使用rpm安装XtraBackup问题总结

    在Red Hat Enterprise Linux Server release 5.7 (Tikanga)上使用RPM方式安装Percona Xtrabackup 2.4.6时遇到了一些问题,特意总 ...

  2. mysql练习----The JOIN operation

    game id mdate stadium team1 team2 1001 8 June 2012 National Stadium, Warsaw POL GRE 1002 8 June 2012 ...

  3. setfont()函数

    设计字体显示效果 Font mf = new Font(String 字体,int 风格,int 字号); 字体:TimesRoman, Courier, Arial等 风格:三个常量 lFont.P ...

  4. Oracle EBS FA 本年折旧

    FUNCTION get_ytd_deprn(p_asset_id IN NUMBER, p_book_type_code IN VARCHAR2, p_rate_source_rule IN VAR ...

  5. 自动化测试基础篇--Selenium Xpath定位

    摘自https://www.cnblogs.com/sanzangTst/p/7458056.html 学习 什么是xpath? XPath即为XML路径语言,它是一种用来确定XML(标准通用标记语言 ...

  6. c/c++ 通用的(泛型)算法 generic algorithm 总览

    通用的(泛型)算法 generic algorithm 总览 特性: 1,标准库的顺序容器定义了很少的操作,比如添加,删除等. 2,问题:其实还有很多操作,比如排序,查找特定的元素,替换或删除一个特定 ...

  7. March 07th, 2018 Week 10th Wednesday

    Better later than never. 亡羊补牢,时犹未晚. Time and again all of us are told to complete the tasks assigned ...

  8. voinc+vue实现级联选择

    需求: vonic中实现级联选择 <!DOCTYPE html> <html> <head> <title>下拉框</title> < ...

  9. eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(一)

    eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(一) 本篇博客主要包含五个内容: 1.CXF换将搭建以及eclipse配置CXF. 2.eclipse创建w ...

  10. 生成.project、.classpath文件

    新导出的项目如果没有.project文件,是不能导入到工具中的,需要执行maven命令生产,在进入工作空间下面,按住shift再点击鼠标右键,点击选择在此处打开命令窗口. 然后输入对应的maven命令 ...