我的这个链表排序思路有问题吗?自测用例通过,但是case一个都没有通过。

单链表的排序

http://www.nowcoder.com/questionTerminal/f23604257af94d939848729b1a5cda08

ListNode* sortInList(ListNode* head){
    ListNode* current = head;
    if(!current||!current->next)
        return current;
    else{
        int currentValue = current->val;
        ListNode* nextNode = current->next;
        int nextValue = nextNode->val;
        if(currentValue<=nextValue)
            current->next=sortInList(current->next);
        else{
            ListNode* temp = nextNode->next;
            current->next = temp;
            nextNode->next = sortInList(current);
            current = nextNode;
        }
        return current;
    }

}

实现逻辑是:

  1. 对于只有一个节点的链表直接返回值
  2. 对于有两个以上节点的,我先取前两个节点Current,NextNode,找到更小的节点,比如Current,此节点的下一个节点就是剩下排序之后的结果(Cuttent->next = sortInList(NextNode)),否则交换节点当前最小元素指向下一个节点为交换后的链表排序结果。
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 11:30
找工作7个月,投了7000封,3段世界五百强实习,才有一个offer,牛油们肯定比我强吧
码农索隆:不对不对不对,实习经历这么厉害,简历也没少投,问题出在哪呢
点赞 评论 收藏
分享
程序员小白条:太晚了,看别人找到实习了才投的话,自己本身就没啥准备,计划太晚咯,只能吞苦果子
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务