老哥们能给我看看我这题《链表排序》哪里有问题吗?

# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def sortList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head or not head.next:
            return head
        head1 = head
        head2 = self.split(head)
        self.sortList(head1)
        self.sortList(head2)
        return self.merge(head1, head2)
    
    def split(self, head):
        if not head:
            return head
        slow, fast = head, head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
        head2 = slow.next
        slow.next = None
        return head2
    
    def merge(self, head1, head2):
        if not head1:
            return head2
        if not head2:
            return head1
        dummy = ListNode(73)
        p = dummy
        while head1 and head2:
            if head1.val <= head2.val:
                p.next = head1
                head1 = head1.next
            else:
                p.next = head2
                head2 = head2.next
            p = p.next
        if head1:
            p.next = head1
        if head2:
            p.next = head2
        return dummy.next

a = [4 , 2, 1, 3]
def build_list(nums):
    if not nums:
        return None
    cur_node = ListNode(nums[0])
    cur_node.next = build_list(nums[1:])
    return cur_node
b = build_list(a)
c = Solution()
d = c.sortList(b)
是leetcode上的一个题,把一个链表排序,我看了半天也没看出问题。
http://www.pythontutor.com/visualize.html#mode=edit 可视化尝试debug下,在某一步return时直接把我一个节点吃了,可我明明没有返回错值。
请大佬们抽个一分钟看看,谢谢了。可视化图在这儿:第一个图当前函数返回前, 第二个图是当前函数返回后,我的2节点不见了,这是为什么?


#leetcode##笔试题目##算法工程师#
全部评论
把sortlist中间两次出现的sortlist函数放到最后的返回值里面return self.mergr(self.sortlist(head1),self.sortlist(head2))😅😅
点赞 回复 分享
发布于 2019-04-18 23:59
自己顶一下,我学生物的,老哥们帮帮忙,谢了
点赞 回复 分享
发布于 2019-04-18 22:35

相关推荐

来个厂收我吧:首先,市场侧求职我不是很懂。 但是,如果hr把这份简历给我,我会觉得求职人不适合做产品经理。 问题点: 1,简历的字体格式不统一,排版不尽如人意 2,重点不突出,建议参考star法则写个人经历 3,印尼官方货币名称为印度尼西亚卢比(IDR),且GMV690000印尼盾换算为305人民币,总成交额不高。 4,右上角的意向职位在发给其他公司时记得删除。 5,你所有的经历都是新媒体运营,但是你要投市场营销岗位,jd和简历不匹配,建议用AI+提示词,参照多个jd改一下经历内容。 修改建议: 1,统一字体(中文:思源黑体或微软雅黑,英文数字:time new romans),在word中通过表格进行排版(b站学) 2,校招个人经历权重:实习经历=创业经历(大创另算)>项目经历>实训经历>校园经历 3,请将项目经历时间顺序改为倒序,最新的放最上方。 4,求职方向不同,简历文字描述侧重点也需要不同。
点赞 评论 收藏
分享
机械打工仔:我来告诉你原因,是因为sobb有在线简历,有些HR为了快会直接先看在线简历,初步感觉不合适就不会找你要详细的了
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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