每日AC--leetcode--recordList
来源:互联网 发布:西北师范知行教务管理 编辑:程序博客网 时间:2024/05/29 17:10
由于最近大脑短路的十分厉害, 表示很无奈
题目描述
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; } }}
阅读全文
0 0
- 每日AC--leetcode--recordList
- 每日AC--LeetCode-Candy
- 每日AC--gas-station--LeetCode
- 每日AC--leetcode-- 2.Add Two Numbers
- 每日AC--LeetCode-- wordBreak--动态规划
- 每日AC
- 每日AC-LeetCode-palindrome-partitioning-ii --动态规划
- 每日AC--LeetCode-Triangle -- 数塔问题DP
- 每日AC--leetCode--PalindromePartitioning--回溯法 递归实现
- 每日AC -数串
- 每日AC-leetcode word-break-ii -与dubbo 异步回调问题
- 每日AC-- 美团 -- 最大差值 | leetcode-求树最小深度 | 网易招聘笔试(升级之路)
- 每日AC-剑指offer-数组查找
- 每日AC--POJ 1458 Common Subsequence -DP
- LeetCode AC代码
- leetcode每日一题
- Leetcode每日一练习
- Leetcode 每日一道
- 分数加减法
- Jquery Table 的基本操作
- HYSBZ
- c++函数重载,重定义,重写,区别详解理解原理
- A Knight's Journey (深度搜索+字典序)
- 每日AC--leetcode--recordList
- HDU 5361 In Touch(并查集实现区间删除)
- vim编辑器-多行加注释与去注释
- Jquery 操作Html 控件 CheckBox、Radio、Select 控件
- dubbo 框架学习-将dubbo 服务做成可执行的java 应用
- 用MsgWaitForMultipleObjects代替WaitForSingleObject和WaitForMultipleObjects()
- 迷茫
- 每日随笔_Animation_Animator
- HDOJ3594-Cactus(仙人掌图)