leetcode reorderList Given{1,2,3,4}, reorder it to{1,4,2,3}.
来源:互联网 发布:mysql主键有什么用 编辑:程序博客网 时间:2024/05/29 06:43
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}.
解题思想:
1.将原链表从中间一分为二,得到left,right两个子链表
2.对于left链表元素顺序不变,right链表反转
3.将right的表头和left的表头逐一取出,拼接在一起形成新表
例如:7,3,8,7,9,1,6,2,10
left
7--3--8--7---9
right
10--2--6--1
插入步骤:
7-10
7-10-3-2
7-10-3-2-8-6
7-10-3-2-8-6-7-1
7-10-3-2-8-6-7-1-9
java代码实现:
/** * 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 temp=middlePos(head); ListNode right=temp.next; right=inverseList(right); ListNode left=head; temp.next=null; ListNode cur=head=new ListNode(-1); while(right!=null){ ListNode leftnex=left.next; ListNode rightnex=right.next; left.next=null; right.next=null; cur.next=left; cur.next.next=right; cur=cur.next.next; left=leftnex; right=rightnex; } if(left!=null){ cur.next=left; } } //cur.next=null; } public ListNode middlePos(ListNode head){ //find the right half list ListNode low=head; ListNode fast=head; while(fast!=null && fast.next!=null){ low=low.next; fast=fast.next.next; } return low; } public ListNode inverseList(ListNode head){ ListNode front=null; ListNode cur=null; while(head!=null){ cur=head; head=head.next; cur.next=null; cur.next=front; front=cur; } return front; }}
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}.
0 0
- leetcode reorderList Given{1,2,3,4}, reorder it to{1,4,2,3}.
- leetCode Given an integer (signed 32 bits), write a function to check whether it is a power of 4
- leetcode题目4:Reorder List
- given a single link list (l0, l1, l2, l3,,,ln), and transform it to (l0, ln, l1, ln-2, l2, ln-3)
- Reorderlist
- ReorderList
- TypeError: playMovVideos() takes exactly 2 arument but 3 to given的解决方法
- Add 1 to a given number
- LeetCode 143 Reorder List Add to List
- Given a roman numeral, convert it to an integer.
- leetcode Read N Characters Given Read 4 I/II
- 题目1 : String reorder
- TypeError: add_or_update() takes at most 3 arguments (4 given)
- MonkeyRunner_TypeError: __init__() takes at least 3 arguments (2 given)
- TypeError: print_space() takes exactly 2 arguments (3 given)
- TypeError: distinct() takes exactly 2 arguments (3 given)
- TypeError: __init__() takes exactly 2 arguments (3 given)
- PAMIE TypeError: getElementsByTagName() takes exactly 1 positional argument (2 given)
- 大数据概念,数据技术分类,ETL
- could not extract ResultSet
- PoEdu_Windows_Lesson02_数据类型和字符编码
- Android 出现错误:Error:(1, 0) Minimum supported Gradle version is 3.3. Current version is 2.14.1. If usi
- HTML+CSS+JS+Ajax技巧篇
- leetcode reorderList Given{1,2,3,4}, reorder it to{1,4,2,3}.
- 论初次修改 Android framework 代码
- Java-java中jvm原理详解
- Linux下sonarqube安装使用方法(代码check工具)
- Android自定义View--简单实现凹凸优惠券效果
- 为什么你们说好的程序在我的海思开发板上就是不行呢,难道真的是人品有问题
- 经典排序算法之冒泡、选择和插入排序
- php谷歌翻译
- 基于Python的select方式的聊天室应用实例