每日AC--leetcode--recordList
2017-08-01 09:00
375 查看
由于最近大脑短路的十分厉害, 表示很无奈
Given a singly linked list L: L 0→L 1→…→L n-1→L n,
reorder it to: L 0→L n →L 1→L n-1→L 2→L n-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}.
AC代码:
public class ListNode {
public int val;
public ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
题目描述
Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-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}.
AC代码:
public class ListNode {
public int val;
public ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
/** * 类说明 * * <pre> * Modify Information: * Author Date Description * ============ =========== ============================ * DELL 2017年7月25日 Create this file * </pre> * */ public class LeetCodeRecordList { /** * Given a singly linked list L: L 0→L 1→…→L n-1→L n, reorder it to: L 0→L n →L 1→L n-1→L 2→L n-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 1 2 3 n n-1 n-2 * @param head */ public void reorderList(ListNode head) { // 链表的结构 // node1->node2->node3->null ListNode rear = head; ListNode tmp ; //构建新的链表 尾插法建立链表 while(head != null && head.next != null){ // 先定位到尾节点 前一个 例如这里算 node2 while(rear.next.next!= null){ rear = rear.next; } tmp = rear.next ; //取出尾节点后一个 比如 node3 rear.next = null; // 原队列node2的后一个置为null tmp.next = head.next; head.next = tmp; head = tmp.next; // 下一次循环从 node1 开始 rear = tmp.next; } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ListNode node1 = new ListNode(1); ListNode node2 = new ListNode(2); ListNode node3 = new ListNode(3); ListNode node4 = new ListNode(4); node1.next = node2; node2.next = node3; node3.next = node4; node4.next = null; new LeetCodeRecordList().reorderList(node1); while(node1 !=null){ System.out.println(node1.val); node1 = node1.next; } } }
相关文章推荐
- 每日AC-leetcode word-break-ii -与dubbo 异步回调问题
- 每日AC-LeetCode-palindrome-partitioning-ii --动态规划
- 每日AC--leetcode-- 2.Add Two Numbers
- 每日AC--LeetCode-Candy
- 每日AC--LeetCode-Triangle -- 数塔问题DP
- 每日AC--leetCode--PalindromePartitioning--回溯法 递归实现
- 每日AC - 后缀表达式计算结果-leetcode-evaluate-reverse-polish-notation
- 每日AC--gas-station--LeetCode
- 每日AC--LeetCode-- wordBreak--动态规划
- 每日AC-- 美团 -- 最大差值 | leetcode-求树最小深度 | 网易招聘笔试(升级之路)
- LeetCode每日一篇
- LeetCode每日一练(7) Reverse Integer C++版
- 每日一练之Longest Common Prefix【LeetCode No.14】——求字符串的最长公共前缀
- LeetCode每日一题(2018-01-26)
- leetcode:258 Add Digits-每日编程第二题
- leetcode:232 Implement Queue using Stacks-每日编程第十六题
- 每日AC-剑指Offer 链表中倒数第k个结点
- leetcode——Letter Combinations of a Phone Number 手机按键字母组合(AC)
- leetcode:223 Rectangle Area-每日编程第三十三题
- leetcode_28题——Implement strStr()(采用KMP算法,还没AC,但自己这边测试无误)