Skip to content

Commit 6060d0a

Browse files
链表
1 parent 87346dd commit 6060d0a

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

2.data_structure/2.linked_list.md

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Solution:
3939

4040
### [remove-duplicates-from-sorted-list-ii](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/)
4141

42-
> 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中   没有重复出现的数字。
42+
> 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现的数字。
4343
4444
- 思路:链表头结点可能被删除,所以用 dummy node 辅助删除
4545

@@ -142,7 +142,7 @@ class Solution:
142142

143143
### [reverse-linked-list-ii](https://leetcode-cn.com/problems/reverse-linked-list-ii/)
144144

145-
> 反转从位置  *m*  *n*  的链表。请使用一趟扫描完成反转。
145+
> 反转从位置 *m**n* 的链表。请使用一趟扫描完成反转。
146146
147147
- 思路:先找到 m 处, 再反转 n - m 次即可
148148

@@ -229,55 +229,59 @@ class Solution:
229229
return lt_dummy.next
230230
```
231231

232-
哑巴节点使用场景
233-
234-
> 当头节点不确定的时候,使用哑巴节点
232+
哑巴节点使用场景:当头节点不确定的时候,使用哑巴节点
235233

236234
### [sort-list](https://leetcode-cn.com/problems/sort-list/)
237235

238-
>  *O*(*n* log *n*) 时间复杂度和常数级空间复杂度下,对链表进行排序。
236+
> *O*(*n* log *n*) 时间复杂度和常数级空间复杂度下,对链表进行排序。
239237
240238
- 思路:归并排序,slow-fast找中点
239+
- 为什么看不到排序就排序了?
240+
- 因为递归到链表长度为 1 的时候,才会返回;
241+
- 这时就是排序好的返回了,长度为2;
242+
- 再到后面就是输入排序好的短链表,可以直接使用合并有序链表。
241243

242244
```Python
243245
class Solution:
244-
245-
def _merge(self, l1, l2):
246-
tail = l_merge = ListNode()
247-
248-
while l1 is not None and l2 is not None:
249-
if l1.val > l2.val:
250-
tail.next = l2
251-
l2 = l2.next
246+
247+
def _merge(self, head1: ListNode, head2: ListNode):
248+
dummy = ListNode()
249+
pointer = dummy
250+
curr1, curr2 = head1, head2
251+
while curr1 or curr2:
252+
if curr1 is None:
253+
pointer.next = curr2
254+
curr2 = curr2.next
255+
elif curr2 is None:
256+
pointer.next = curr1
257+
curr1 = curr1.next
252258
else:
253-
tail.next = l1
254-
l1 = l1.next
255-
tail = tail.next
259+
if curr1.val < curr2.val:
260+
pointer.next = curr1
261+
curr1 = curr1.next
262+
else:
263+
pointer.next = curr2
264+
curr2 = curr2.next
265+
pointer = pointer.next
266+
return dummy.next
256267

257-
if l1 is not None:
258-
tail.next = l1
259-
else:
260-
tail.next = l2
261-
262-
return l_merge.next
263-
264-
def _findmid(self, head):
268+
def _find_mid(self, head: ListNode):
265269
slow, fast = head, head.next
266-
while fast is not None and fast.next is not None:
267-
fast = fast.next.next
270+
while fast and fast.next:
268271
slow = slow.next
269-
272+
fast = fast.next.next
270273
return slow
271-
272-
def sortList(self, head: ListNode) -> ListNode:
274+
275+
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
273276
if head is None or head.next is None:
274277
return head
275-
276-
mid = self._findmid(head)
277-
tail = mid.next
278-
mid.next = None # break from middle
279-
280-
return self._merge(self.sortList(head), self.sortList(tail))
278+
mid = self._find_mid(head)
279+
sub_head = mid.next
280+
mid.next = None
281+
return self._merge(
282+
self.sortList(head),
283+
self.sortList(sub_head),
284+
)
281285
```
282286

283287
注意点

0 commit comments

Comments
 (0)