美文网首页
2019-06-08LeetCode 147. 对链表进行插入排

2019-06-08LeetCode 147. 对链表进行插入排

作者: mztkenan | 来源:发表于2019-06-08 22:26 被阅读0次

1.指针未归零,造成死循环。
2.输出错误,导致不知道哪里错了

class Solution:
    def insertionSortList(self, head: ListNode) -> ListNode:
        res =ListNode(None)
        while (head != None):
            cur = head
            head = head.next
            cur.next=None #指针归零很重要,不然会造成循环指针,这里三个顺序不能变


            tmp = res.next
            pre = res
            while tmp != None and tmp.val<cur.val:
                pre=pre.next
                tmp = tmp.next
            cur.next = tmp
            pre.next = cur
        return res.next

相关文章

网友评论

      本文标题:2019-06-08LeetCode 147. 对链表进行插入排

      本文链接:https://www.haomeiwen.com/subject/mlpwxctx.html