LeetCode(143): Recorder List

Recorder List: Given a singly linked list L: L0L1→…→Ln-1Ln,reorder it to: L0LnL1Ln-1L2Ln-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}.

题意:给定一个链表,把最后一个结点插入到第一个结点后面,倒数第二个结点插入到原链表第二个结点后面,依次类推。

思路:利用快慢指针的方法找到链表的中点,然后逆转中点右边的链表,然后将两个链表合并。

代码:

public void reorderList(ListNode head) {
        if(head == null || head.next == null) return;

        ListNode slow = head;
        ListNode fast = head;
        while(fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }

        ListNode mid = slow.next;
        ListNode cur = mid;
        ListNode pre = null;
        while(cur != null){
            ListNode post = cur.next;
            cur.next = pre;
            pre = last;
            cur = post;
        }
        slow.next = null;

        while(head != null && pre != null){
            ListNode next1 = head.next;
            head.next = pre;
            pre = pre.next;
            head.next.next = next1;
            head = next1;
        }

    }
时间: 2024-07-28 21:29:16

LeetCode(143): Recorder List的相关文章

Java for LeetCode 143 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}. 解题思路一: 每次将Ln换到前面,得到L0→Ln→L1→L2→L3→,然后对L1使用相同操作,

LeetCode 143题:Reorder List的分析及两种解题思路

特别说明:参考了很多前辈的文章,整理如下,我只做了重新编码的工作,不能保证代码最优,主要用作交流学习. 题目: 方法1:将链表的每个节点地址保存在指针数组中,利用数组随机访问调整链表. 1 struct ListNode 2 { 3 int val; 4 ListNode *next; 5 ListNode(int x) : val(x), next(NULL) {} 6 }; 7 8 void reorderList(ListNode *head) 9 { 10 if (head == nul

leetcode 143. Reorder List ----- java

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.使用list记录链表. /** * Definition for si

leetcode || 143、Reorder List

problem: 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}. Hide Tags Linked List 题意:对单链表重新排序,依次将尾

[leetcode]143. 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 may not modify the values in the list's nodes, only nodes itself may be changed. Example 1: Given 1->2->3->4, reorder it to 1->4->2->3. Example 2: G

Leetcode 143.重排链表

重排链表 给定一个单链表 L:L0→L1→-→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→- 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例 1: 给定链表 1->2->3->4, 重新排列为 1->4->2->3. 示例 2: 给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3. 1 public class Solution{ 2

【Leetcode解题报告】单链表结点位置调整

Leetcode 206 Reverse Linked List 题目描述 Reverse a singly linked list. 分析与解法 (1) 递归解法 参考代码如下: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { pu

LeetCode链表解题模板

一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改,比如要计算链表的长度: ListNode* p = head; int num = 0; while(p!=NULL){ num++; p = p->next; } 如果要找到最后的节点,可以更改while循环中的条件,只不过需要加上head为NULL时的判断 if(!head) return hea

LeetCode: Reorder List [143]

[题目] 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}. [题意] 给定一个链表L: L0→L1→-→Ln-1→Ln,对他重新排序成L0→Ln