LeetCode OJ-- Reorder List **
https://oj.leetcode.com/problems/reorder-list/
将一个链表重新排序,比如 1 2 3 4 5,变成 1 5 2 4 3
1.找到中间节点 mid
2.将链表分成两部分 head,head2
3.将head2 逆序
4.将head head2,再合并成一个链表
- class Solution {
- public:
- void reorderList(ListNode *head) {
- //0 elements, 1 elements, 2 elements
- if(head == NULL || head != NULL && head->next == NULL || head!= NULL && head->next != NULL && head->next->next == NULL)
- return;
- //find middle
- ListNode *end = head;
- ListNode *mid = head;
- while(end)
- {
- end = end->next;
- if(end && end->next)
- {
- end = end->next;
- mid = mid->next;
- }
- }
- ListNode *head2 = mid->next;
- //split
- mid->next = NULL;
- //reverse head2
- ListNode *itr = head2;
- if(head2->next)
- {
- head2 = head2->next;
- itr->next = NULL;
- ListNode *tmp = NULL;
- while(head2)
- {
- tmp = head2;
- head2 = head2->next;
- tmp->next = itr;
- itr = tmp;
- }
- head2 = itr;
- }
- //union
- ListNode *dummy = new ListNode();
- ListNode *node1 = head;
- while(node1)
- {
- dummy->next = node1;
- node1 = node1->next;
- dummy = dummy->next;
- if(head2)
- {
- dummy->next = head2;
- head2 = head2->next;
- dummy = dummy->next;
- }
- else
- break;
- }
- }
- };
LeetCode OJ-- Reorder List **的更多相关文章
- [LeetCode OJ] Reorder List—Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * str ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- LeetCode OJ 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- LeetCode OJ 之 Maximal Square (最大的正方形)
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- LeetCode OJ:Integer to Roman(转换整数到罗马字符)
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
随机推荐
- Base64及其Python实现
1. 什么是Base64 Base64是一种基于64个可打印字符来表示二进制数据的表示方法 Base64是一种编码方式,提及编码方式,必然有其对应的字符集合.在Base64编码中,相互映射的两个集合是 ...
- 最小生成树:HDU1863-畅通工程
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- 多进程的基本使用--multiprocessing 【转】
multiprocessing 如果你打算编写多进程的服务程序,Unix/Linux无疑是正确的选择.由于Windows没有fork调用,难道在Windows上无法用Python编写多进程的程序? 由 ...
- 利用本地SQL Server维护计划来维护SQL Database
On-Premise的SQL Server提供了维护计划来定期.定时的维护SQL Server.一般的做法是:定义SQL Server Agent Jobs,而后维护计划帮助我们定期.定时执行SQL ...
- mysql-copy to tmp table
今天数据后台数据反映有些迟缓后查看链接 processlist 发下好多 锁 和磁盘写入, 参考文章 : http://bbs.chinaunix.net/forum.php?mod=viewth ...
- VMware之无法切换桥接网络
一.关闭正在运行的虚拟机 二.打开虚拟网络编辑器 三.还原默认设置 四.启动虚拟机即可正常使用桥接网络
- WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决
原文:WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决 如下图,在凭证编辑窗体中,有的单元格不需要数字,但如果录入数字后再删除,会触发数字验证,单元格显示红色框线,导致不能执行 ...
- easyui的layout
1.浏览器自适应(即浏览器改变大小,里面的表格大小也会随之改变)要设置两个参数 (1)一般都要在body上设置class=“easyui-layout”: <body class="e ...
- 二叉树遍历(Java实现)
二叉树遍历(Java实现) 主要是二叉树的遍历,包括递归遍历和非递归遍历 import java.util.ArrayDeque; import java.util.ArrayList; impo ...
- Python框架之Django学习笔记(十五)
表单 从Google的简朴的单个搜索框,到常见的Blog评论提交表单,再到复杂的自定义数据输入接口,HTML表单一直是交互性网站的支柱.本次内容将介绍如何用Django对用户通过表单提交的数据进行访问 ...