leetcode--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}
.
/*** Definition for singly-linked list.* class ListNode {* int val;* ListNode next;* ListNode(int x) {* val = x;* next = null;* }* }*/
public class Solution {public void reorderList(ListNode head) {if(head != null){ListNode stepOne = head, stepTwo = head;while(stepTwo.next != null){if(stepTwo.next != null)stepTwo = stepTwo.next;if(stepTwo.next != null){stepTwo = stepTwo.next;stepOne = stepOne.next;}}if(stepOne.next != null){stepTwo = stepOne.next;stepOne.next = null;ListNode tailOfSecond = stepTwo;stepOne = stepTwo;if(stepTwo.next != null)stepTwo = stepTwo.next;while(stepTwo.next != null){ListNode temp = stepTwo.next;stepTwo.next = stepOne;stepOne = stepTwo;stepTwo = temp;}stepTwo.next = stepOne;tailOfSecond.next = null;}stepOne = head;if(stepTwo != stepOne){while(stepTwo != null){ListNode temp = stepOne.next;stepOne.next = stepTwo;stepOne = stepOne.next;stepTwo = temp;}}} }
}
转载于:https://www.cnblogs.com/averillzheng/p/3552936.html
leetcode--Reorder List相关推荐
- LeetCode - Reorder List
Reorder List 2014.1.13 22:07 Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→ ...
- [Leetcode] 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: Reorder List
FROM 思路 1. 将后半段截取下来再倒序, 插入到前半段, 时间复杂度为 o(n) 代码 #include <iostream> using namespace std;struct ...
- LeetCode Reorder List
题意:给出一个链表,重新排列链表,形式为第一个-->倒数第一个->第二个->倒数第二个 思路:将数组分成两半,然后将后半部反转,再拼接 代码发下: class Solution {p ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- [LeetCode] 143. Reorder List_Middle tag: Linked 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 mo ...
- 【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 143. Reorder List
LeetCode 143. Reorder List Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/4254860.html 这段代码有值得学习 ...
- leetcode之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:937. Reorder Log Files
051401 题目 You have an array of logs. Each log is a space delimited string of words. For each log, th ...
最新文章
- OCP读书笔记(5) - 使用RMAN创建备份
- CCNA学习笔记--静态路由
- Android多线程----异步消息处理机制之Handler
- git config设置用户名_一个神奇的工具,实现多人协作,git常用命令详解
- 某业务付费统计脚本问题排查
- Ocelot统一权限验证
- 435. 无重叠区间(贪心经典题+思路+详解)
- Codeforces Round #462 (Div. 2), problem: (C) A Twisty Movement (求可以转一次区间的不递增子序列元素只有1,2)...
- 力扣-692 前k个高频单词
- 数字头盔摄像头是一个智能选项
- 时间序列分析:使用Pandas探索能源数据集
- 2021美亚杯第七届中国电子数据取证团体赛题目详解write up
- python elif invalid syntax_Python错误集锦:if和elif语句提示:SyntaxError: invalid syntax
- C语言学习-- 计算机原理及二进制
- pandas已知数值怎么找位置_pandas.dataframe中根据条件获取元素所在的位置方法(索引)...
- WPS删除粘贴后的[]中括号痕迹
- php7.2.3下载,最新PHP编程软件v7.3.2.0 官方版下载地址电脑版-锐品软件
- ORACLE+10G+win7下载地址
- 我眼里的奇酷手机360OS
- 2016年8月2日 星期二 --出埃及记 Exodus 16:2