【Leetcode】【Medium】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 this in-place without altering the nodes' values.
For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.
解题思路:
设置一个指针mid指向链表的中间结点;
将mid之后的半个链表进行反转,反转后的链表insert;
将insert中的结点挨个插入前半段链表中;
代码:
/**
* 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) {
if (!head || !head->next || !head->next->next)
return;
ListNode* ctrl = head;
ListNode* mid = head;
ListNode* insert = NULL; while (ctrl->next && ctrl->next->next) {
ctrl = ctrl->next->next;
mid = mid->next;
} insert = reverseList(mid->next);
mid->next = NULL;
ctrl = head; while (insert) {
ListNode* t = ctrl->next;
ctrl->next = insert;
insert = insert->next;
ctrl->next->next = t;
ctrl = t;
} return;
} ListNode* reverseList(ListNode* head) {
if (!head->next)
return head;
ListNode* cur = NULL;
ListNode* next = head;
ListNode* left = NULL;
while (next) {
left = next->next;
next->next = cur;
cur = next;
next = left;
}
return cur;
}
};
【Leetcode】【Medium】Reorder List的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
- 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum
[Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...
- 【LeetCode题意分析&解答】38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
随机推荐
- python+selenium打开浏览器
Firefox(高版本要安装换火狐驱动,47版本以下可不安装) GoogleChrome(需要安装浏览器的驱动插件,驱动到selenium官网下载,目前该浏览器的驱动只有32位的,所以Google安装 ...
- vue element 常见问题
1. vue2.0 给data对象新增属性,并触发视图更新 $set this.$set(this.ossData, "signature", 222) // 正确用法 // 数 ...
- python+unittest+requests实现接口自动化
前言: Requests简介 Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urllib2 ...
- android httpclient 发送 PATCH 请求
Put 用于全部更新 Patch 用于部分更新 重写一下 HttpPut 或 HttpPost 的 getMethod 方法 /** * Send a patch request */ public ...
- Mongodb cassandra 和 Mysql对比
MongoDBDB.Cassandra和 Mysql对比 1.为什么是Nosql? 1.1 Nosql在大数据处理相对于关系型数据库具有优势 1.1.1 1. 低延迟 ...
- MacOS系统下的图形化工具
MacOS系统下的图形化工具 MacOS系统下安装了Git后,发现如果Git中有中文文档操作还是比较麻烦(需要输入中文的文件名).图形化对Git的操作还是相对于方便一些.所以准备找一个图形化的工具. ...
- spring 配置文件被加载两次
如下web.xml示例: 1.用spring的配置加载contextConfigLocation 2.配置spring-mvc的contextConfigLocation <servlet> ...
- 用eclipse查看JDK源代码
把jdk的源代码导入eclipse
- The “SignFile” task was not given a value for the required parameter “CertificateThumbprint”的一个简单的解决方法
这个只是其中一种解决方法,而且不是万能的 1. 由提示内容可以看出,这个一个 sign(认证)的问题, 在出现这个问题的项目上,鼠标右键,选择properties,然后选择signing. 2. 选择 ...
- SQL Developer 改成英文