(多种方法)重排链表

题目:

(多种方法)重排链表_第1张图片

题解一:(队列法)

public ListNode reorderList(ListNode head) {
        ListNode pre = head;
        ListNode cur = head;
        LinkedList<ListNode> listNodes = new LinkedList<ListNode>();
        // 将节点加入队列
        while (pre != null){
            listNodes.add(pre);
            pre = pre.next;
        }
        ListNode post = null;
        while (!listNodes.isEmpty() || (pre != null || post != null)){
            // 从首尾拿出
            pre = listNodes.removeFirst();
            post = listNodes.removeLast();
            // 判断是否是首节点
            if (pre == cur){
                pre.next = post;
                cur = post;
            }else {
                cur.next = pre;
                pre.next = post;
                cur = post;
            }
            // 将两个指针置为null,目的是为了上层while循环条件
            pre = post = null;
        }
        // 将最后一个节点的指针next为null
        if (cur != null) cur.next = null;
        return head;
    }

方法二:(拆解链表,反转链表,链表合并)

public class Code {

    public ListNode reorderList(ListNode head) {
        ListNode first = head;
        // 寻找中间节点
        ListNode node = findMiddleNode(first);
        ListNode middleNode= node.next;
        node.next = null;

        // 翻转节点
        ListNode l1 = head;
        ListNode l2 = reverseNode(middleNode);

        // 合并节点
        ListNode mergedHead = mergeNode(l1, l2);
        return mergedHead;
    }

    /**
     * 寻找中间节点
     * @param head
     * @return 中间节点
     */
    public ListNode findMiddleNode(ListNode head){
        ListNode slow = head;
        ListNode fast = head;
        while (fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

    /**
     * 翻转链表
     * @param head
     * @return 反转后的头节点
     */
    public ListNode reverseNode(ListNode head){
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null){
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }

    /**
     * 合并两个链表
     * @param l1
     * @param l2
     * @return 返回头节点
     */
    public ListNode mergeNode(ListNode l1, ListNode l2){
        ListNode head = l1;
        while (l1 != null && l2 != null){
            ListNode l1Temp = l1.next;
            ListNode l2Temp = l2.next;
            l1.next = l2;
            l1 = l1Temp;
            l2.next = l1;
            l2 = l2Temp;
        }
        return head;
    }

    public static void main(String[] args) {
        Code code = new Code();
        ListNode a = new ListNode(1);
        ListNode b = new ListNode(2);
        ListNode c = new ListNode(3);
        ListNode d = new ListNode(4);
        a.next = b;
        b.next = c;
        c.next = d;
        ListNode listNode = code.reorderList(a);
        while (listNode != null){
            System.out.print(listNode.val + " ");
            listNode = listNode.next;
        }
    }

}

你可能感兴趣的:(力扣刷题,链表,list,数据结构)