チェーンテーブル、奇数ビット昇順偶数ビット降順、チェーンテーブルを昇順にする

4038 ワード

タイトル説明:チェーンテーブル、奇数ビット昇順偶数ビット降順、チェーンテーブルを昇順にします.例えば、1 8 3 6 5 4 7 2 9、最後に1 2 3 4 6 8 9を出力します.分析:この問題は3つのステップに分けることができます:まず奇数位と偶数位によって2つのチェーンテーブルに分割します.次に偶数チェーンテーブルを反転します.最後に、2つの順序付きチェーンテーブルをマージします.
package com.test;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        ListNode head = main.initList();
        main.printList(head);
        ListNode[] heads = main.splitList(head);
        main.printList(heads[0]);
        main.printList(heads[1]);
        ListNode reverseHead = main.reverseList(heads[1]);
        main.printList(reverseHead);
        ListNode mergeHead = main.mergeLists(heads[0], reverseHead);
        main.printList(mergeHead);
    }

    //            
    private ListNode[] splitList(ListNode head) {
        ListNode head1 = null;
        ListNode head2 = null;
        ListNode cur1 = null;
        ListNode cur2 = null;

        int count = 1;
        while (head != null) {
            if (count % 2 == 1) {
                if (cur1 != null) {
                    cur1.next = head;
                    cur1 = cur1.next;
                } else {
                    cur1 = head;
                    head1 = cur1;
                }
            } else {
                if (cur2 != null) {
                    cur2.next = head;
                    cur2 = cur2.next;
                } else {
                    cur2 = head;
                    head2 = cur2;
                }
            }
            head = head.next;
            ++count;
        }
        //    ,                 null
        cur1.next = null;
        cur2.next = null;
        ListNode[] heads = new ListNode[]{head1, head2};
        return heads;
    }

    //    
    private ListNode reverseList(ListNode head) {
        ListNode cur = head;
        ListNode pre = null;
        ListNode next = null;
        while (cur != null) {
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }

    //        
    private ListNode mergeLists(ListNode head1, ListNode head2) {
        if (head1 == null && head2 == null) {
            return null;
        }
        if (head1 == null || head2 == null) {
            return head1 == null ? head2 : head1;
        }
        ListNode first = new ListNode(-1);
        ListNode cur = first;
        while (head1 != null && head2 != null) {
            if (head1.val < head2.val) {
                cur.next = head1;
                head1 = head1.next;
            } else {
                cur.next = head2;
                head2 = head2.next;
            }
            cur = cur.next;
        }
        cur.next = head1 != null ? head1 : head2;
        return first.next;
    }

    //     
    private ListNode initList() {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(8);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(6);
        ListNode node5 = new ListNode(5);
        ListNode node6 = new ListNode(4);
        ListNode node7 = new ListNode(7);
        ListNode node8 = new ListNode(2);
        ListNode node9 = new ListNode(9);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        node6.next = node7;
        node7.next = node8;
        node8.next = node9;
        return node1;
    }

    //    
    private void printList(ListNode head) {
        if (head == null) {
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            System.out.print(cur.val + "\t");
            cur = cur.next;
        }
        System.out.println(cur.val);
    }
}

class ListNode {
    public int val;
    public ListNode next;

    public ListNode() {

    }

    public ListNode(int val) {
        this.val = val;
        this.next = null;
    }
}

 
転載先:https://www.cnblogs.com/xidian2014/p/8652632.html