微软算法面试题链表排序:奇数位升序,偶数位降序,重排整体为升序


/**
 * 给定一个链表:1 10 5 3 8
 * 其中,其奇数位是升序,偶数位是降序。
 * 要求对其进行重新排序,整体是升序的。
 *
 * 答:解题思路:
 * 按奇偶位置拆分链表,得1->5->8->NULL和10->3->NULL
 * 反转偶数位组成的链表,得1->5->8->NULL和3->10->NULL
 * 合并两个有序链表,得1->3->5->8->10->NULL
 */
public class Solution {
    public static void main(String[] args) {
        ListNode head = init();
        ListNode[] lists = getLists(head); //按奇偶位置拆分链表

        ListNode head1 = lists[0];
        ListNode head2 = lists[1];

        head2 = reverseList(head2); //反转偶数位组成的链表

        head = Merge(head1, head2); //合并两个升序链表

    }

    public static ListNode init() {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(10);
        ListNode node3 = new ListNode(5);
        ListNode node4 = new ListNode(3);
        ListNode node5 = new ListNode(8);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        return node1;
    }


    /**
     * 按奇偶位置拆分链表
     *
     * @param head
     * @return
     */
    public static ListNode[] getLists(ListNode head) {

        ListNode head1 = null;
        ListNode head2 = null;
        int count = 1;
        ListNode cur1 = null;
        ListNode cur2 = null;

        while (head != null) {
            if (count % 2 == 1) {
                if (head1 == null) {
                    cur1 = head;
                    head1 = cur1;
                } else {
                    cur1.next = head;
                    cur1 = cur1.next;
                }

            } else {
                if (head2 == null) {
                    cur2 = head;
                    head2 = cur2;
                } else {
                    cur2.next = head;
                    cur2 = cur2.next;
                }


            }
            count++;
            head = head.next;

        }
        //跳出循环,要让最后两个末尾元素的下一个都指向null
        cur1.next = null;
        cur2.next = null;
        ListNode[] list = new ListNode[]{head1, head2};

        return list;
    }


    /**
     * 反转偶数位组成的链表
     *
     * @param head
     * @return
     */
    public static ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode next = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

    /**
     * 合并两个升序链表
     * 遍历这两个升序链表list1和list2;
     * 升序:比较两个链表节点的值,谁小取谁做新链表的下一个节点,取谁的节点就往后移位,直到尾部null。
     *
     * @param list1
     * @param list2
     * @return
     */
    public static ListNode Merge(ListNode list1, ListNode list2) {

        if (list1 == null) {
            return list2;
        }
        if (list2 == null) {
            return list1;
        }

        ListNode newList = new ListNode(-1);
        ListNode position = newList;//需要一个节点来记录头节点的位置
        while (list1 != null && list2 != null) {

            if (list1.val <= list2.val) {
                newList.next = list1;
                list1 = list1.next;
                newList = newList.next;
            } else if (list1.val > list2.val) {
                newList.next = list2;
                list2 = list2.next;
                newList = newList.next;
            }
        }

        if (list1 == null) {
            newList.next = list2;
        }

        if (list2 == null) {
            newList.next = list1;
        }
        return position.next;
    }


}

更多干货关注:剑指 Offer (3群)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值