美文网首页
LeetCode-143-重排链表

LeetCode-143-重排链表

作者: 醉舞经阁半卷书 | 来源:发表于2022-01-21 13:20 被阅读0次

重排链表

题目描述:给定一个单链表 L 的头节点 head ,单链表 L 表示为:

L0 → L1 → … → Ln-1 → Ln
请将其重新排列后变为:

L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …

不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

示例说明请见LeetCode官网。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reorder-list/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:链表遍历

首先,如果链表为空或链表只有一个节点,直接返回。

否则,首先用一个栈nodes记录所有的节点,并记录链表节点的数量count;

然后,记录插入的顺序,遍历到奇数位时,从头结点方向插入链表;遍历到偶数位时,从栈中取出节点(即从尾结点方向)插入链表。

import com.kaesar.leetcode.ListNode;

import java.util.Stack;

public class LeetCode_143 {
    public static void reorderList(ListNode head) {
        if (head == null || head.next == null) {
            return;
        }
        // 所有节点
        Stack<ListNode> nodes = new Stack<>();
        // 链表节点的数量
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            nodes.push(cur);
            cur = cur.next;
        }

        int front = 1, back = 0, i = 1;
        ListNode newCur = head;
        cur = head.next;
        // 分别从头结点和栈中遍历链表节点,然后按指定顺序插入新的头节点构成的链表中
        while (front + back < count) {
            i++;
            if (i % 2 == 1) {
                // 插入正向的节点
                newCur.next = cur;
                cur = cur.next;
                front++;
            } else {
                // 插入后面的节点
                newCur.next = nodes.pop();
                back++;
            }
            newCur = newCur.next;
        }
        // 最后,要将新的尾结点的next指向null
        newCur.next = null;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);

        System.out.println("-----重排之前-----");
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();

        reorderList(head);
        System.out.println("-----重排之后-----");
        cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }

    }
}

【每日寄语】 人不怕有理想,不怕有梦想。也不管它又多大,又有多远!只要你客观的认清自己,在道德规范之内,坚持自己,做你想做的,一定会有收获的那一天!

相关文章

  • LeetCode-143-重排链表

    给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln...

  • LeetCode-143-重排链表

    重排链表 题目描述:给定一个单链表 L 的头节点 head ,单链表 L 表示为:L0 → L1 → … → Ln...

  • 143. 重排链表

    143. 重排链表

  • 重排链表

    形如L1->L2->...->Ln的链表,编写函数将链表重新排列成L1->Ln->L2->Ln-1->...,要求...

  • All for PAT秋考 | 1132 - 1135

    涉及知识1132 sscanf(),浮点错误1133 链表重排(cmp函数、假装重排= =)1134 图的点覆盖(...

  • Redis数据结构学习-链表(二)

    链表 链表提供了高效的节点重排能力, 及顺序性节点访问方式, Redis构建了自己的链表实现 链表和链表节点的实现...

  • leetcode链表之重排链表

    143、重排链表[https://leetcode-cn.com/problems/reorder-list/] ...

  • LintCode 重排链表

    题目 给定一个单链表L: L0→L1→…→Ln-1→Ln, 重新排列后为:L0→Ln→L1→Ln-1→L2→Ln-...

  • 链表重排序

    题目要求:对于链表如L1->L2->L3->L4->L5->L6->L7重新排列为L1->L7->L2->L6->...

  • 链表-中等:重排链表 By Swift

    题目 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2...

网友评论

      本文标题:LeetCode-143-重排链表

      本文链接:https://www.haomeiwen.com/subject/hayehrtx.html