4
头图

Rearrange linked list

Topic description: Given a head node head of a singly linked list L, the singly linked list L is expressed as:

L0 → L1 → … → Ln-1 → Ln
Please rearrange it to become:

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

You can't just change the value inside the node, but actually need to exchange the node.

For example descriptions, please refer to the official website of LeetCode.

Source: LeetCode
Link: https://leetcode-cn.com/problems/reorder-list/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.

Solution 1: Linked List Traversal

First, if the linked list is empty or the linked list has only one node, return it directly.

Otherwise, first use a stack nodes to record all nodes, and record the number of linked list nodes count;

Then, record the insertion order. When traversing to the odd bit, insert it into the linked list from the direction of the head node; when traversing to the even bit, remove the node from the stack (that is, from the direction of the tail node) and insert it into the linked list.

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;
        }

    }
}
【Daily Message】 people are not afraid of having ideals and dreams. No matter how big or how far it is! As long as you recognize yourself objectively, stick to yourself, and do what you want to do within the moral code, there will be a day when you will gain something!

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!