【Leetcode】148. Sort List

Sort a linked list in O(n log n) time using constant space complexity.

1 corner case: 当list不存在和head.next不存在时,都返回head就ok了。code为if not head and not head.next: return head

2 tail.next = h1这句code的意思是,将tail这个node和h1这个node连接起来

3 tail.next, tail, h1 = h1, h1, h1.next  执行顺序是先建立h1,h1和h1.next的copy,然后先执行tail.next = h1,建立从tail到h1的连接,接着tail=h1代表h1现在是这个连接的tail,最后的的h1=h1.next代表将h1移动到h1.next node去

4 tail.next= h1 or h2 代表如果h1存在的话,tail.next指向h1,如果h1不存在的话,tail.next指向h2

5 定义一个merge sort函数,找到middle node,将输入linked list分成两部分,然后调用merge sort函数

6 merge函数里定义一个dummy和tail的作用是:dummy守住起始点,tail用来移动,从而merge h1和h2

7 merge中使用while循环,循环条件是h1和h2都不为零

你可能感兴趣的:(【Leetcode】148. Sort List)