老师,麻烦帮我看看我的147号问题的solution可以怎么优化,有3个case没通过,超时了

来源:5-4 复杂的穿针引线 Swap Nodes in Pairs

HuangZhCh

2017-10-20

class Solution {
    public ListNode insertionSortList(ListNode head) {
        if(head == null)
            return head;
        ListNode dummy = new ListNode(java.lang.Integer.MIN_VALUE);
        dummy.next = head;
        ListNode last = dummy.next; //需要插入的节点的上一个节点
        ListNode cur = last.next;   //当前需要插入的节点
        while(cur != null){
            ListNode pre = dummy;  //插入处的前一节点
            ListNode post = pre.next;  //插入处的后一节点
            while(post != cur){
                if(cur.val >= pre.val && cur.val < post.val){
                    last.next = cur.next;
                    cur.next = post;
                    pre.next = cur;
                    break;
                }else{
                    pre = post;
                    post = pre.next;
                }
            }
            last = cur;
            cur = last.next;
        }
        return dummy.next;
    }
}


写回答

1回答

HuangZhCh

提问者

2017-10-20

我这个程序有大问题。。。。已经解决了老师

0
1
liuyubobobo
:)继续加油!
2017-10-21
共1条回复

玩转算法面试-- Leetcode真题分门别类讲解

课程配套大量BAT面试真题,高频算法题解析,强化训练

7435 学习 · 1159 问题

查看课程