@@ -204,6 +204,58 @@ var partition = function(head, x) {
204204
205205##### [ 148. 排序链表] ( https://leetcode-cn.com/problems/sort-list/ )
206206
207+ 给你链表的头结点 ` head ` ,请将其按 ** 升序** 排列并返回 ** 排序后的链表** 。` O(nlogn) ` 时间复杂度,常数级空间。
208+
209+ ``` js
210+ var sortList = function (head ) {
211+ return mergeSort (head)
212+ };
213+
214+ const findMidNode = head => {
215+ let slow = head
216+ let fast = head .next
217+ while (fast !== null && fast .next !== null ) {
218+ slow = slow .next
219+ fast = fast .next .next
220+ }
221+ return slow
222+ }
223+
224+ const mergeTwoList = (l1 , l2 ) => {
225+ const dummy = new ListNode (0 )
226+ let p = dummy
227+ while (l1 !== null && l2 !== null ) {
228+ if (l1 .val < l2 .val ) {
229+ p .next = l1
230+ l1 = l1 .next
231+ } else {
232+ p .next = l2
233+ l2 = l2 .next
234+ }
235+ p = p .next
236+ }
237+ if (l1 !== null ) { // l1不为空,p指向剩下的链表
238+ p .next = l1
239+ }
240+ if (l2 !== null ) {
241+ p .next = l2
242+ }
243+ return dummy .next
244+ }
245+
246+ const mergeSort = head => {
247+ if (head === null || head .next === null ) {
248+ return head
249+ }
250+ let mid = findMidNode (head)
251+ let tail = mid .next
252+ mid .next = null
253+ let left = mergeSort (head)
254+ let right = mergeSort (tail)
255+ return mergeTwoList (left, right)
256+ }
257+ ```
258+
207259##### [ 143. 重排链表] ( https://leetcode-cn.com/problems/reorder-list/ )
208260
209261给定一个单链表 L:L0→L1→…→Ln-1→Ln ,
@@ -253,3 +305,136 @@ var hasCycle = function (head) {
253305}
254306```
255307
308+ ##### [ 142. 环形链表 II] ( https://leetcode-cn.com/problems/linked-list-cycle-ii/ )
309+
310+ 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 ` null ` 。
311+
312+ ``` js
313+ var detectCycle = function (head ) {
314+ let visited = new Set () // 使用哈希表,空间复杂度O(n)
315+ while (head !== null ) {
316+ if (visited .has (head)){
317+ return head
318+ }
319+ visited .add (head)
320+ head = head .next
321+ }
322+ return null
323+ };
324+ ```
325+
326+ ``` js
327+ var detectCycle = function (head ) {
328+ if (head === null || head .next === null ) {
329+ return null
330+ }
331+ let slow = head
332+ let fast = head .next
333+ while (fast !== null && fast .next !== null ){
334+ if (fast === slow) { // 数学推导,看官网题解
335+ slow = slow .next
336+ fast = head
337+ while (fast !== slow){
338+ slow = slow .next
339+ fast = fast .next
340+ }
341+ return slow
342+ }
343+ slow = slow .next
344+ fast = fast .next .next
345+ }
346+ return null
347+ };
348+ ```
349+
350+ ##### [ 234. 回文链表] ( https://leetcode-cn.com/problems/palindrome-linked-list/ )
351+
352+ 请判断一个链表是否为回文链表。
353+
354+ ``` js
355+ var isPalindrome = function (head ) {
356+ if (head === null || head .next === null ) {
357+ return true
358+ }
359+ let slow = head
360+ let fast = head .next
361+ while (fast !== null && fast .next !== null ) {
362+ slow = slow .next
363+ fast = fast .next .next
364+ }
365+ let tail = reverseList (slow .next )
366+ slow .next = null
367+ while (head !== null && tail !== null ) {
368+ if (head .val !== tail .val ) {
369+ return false
370+ }
371+ head = head .next
372+ tail = tail .next
373+ }
374+ return true
375+ };
376+
377+ const reverseList = head => {
378+ if (head === null ) {
379+ return head
380+ }
381+ const dummy = new ListNode (0 )
382+ let prev = head
383+ while (prev !== null ) { // 头插法
384+ let temp = prev
385+ prev = prev .next
386+ temp .next = dummy .next
387+ dummy .next = temp
388+ }
389+ return dummy .next
390+ }
391+ ```
392+
393+ ##### [ 138. 复制带随机指针的链表] ( https://leetcode-cn.com/problems/copy-list-with-random-pointer/ )
394+
395+ 给你一个长度为 ` n ` 的链表,每个节点包含一个额外增加的随机指针 ` random ` ,该指针可以指向链表中的任何节点或空节点。
396+
397+ ``` js
398+ var copyRandomList = function (head ) {
399+ if (head === null ) {
400+ return head
401+ }
402+ let cur = head
403+ while (cur !== null ) {
404+ const clone = new Node (cur .val , cur .next , cur .random )
405+ const temp = cur .next
406+ cur .next = clone
407+ cur = temp
408+ }
409+ cur = head
410+ while (cur !== null ) {
411+ if (cur .random !== null ) {
412+ cur .next .random = cur .random .next
413+ }
414+ cur = cur .next .next
415+ }
416+ cur = head
417+ let cloneHead = cur .next
418+ while (cur !== null && cur .next !== null ) {
419+ const temp = cur .next
420+ cur .next = cur .next .next
421+ cur = temp
422+ }
423+ return cloneHead
424+ };
425+ ```
426+
427+ ## 练习
428+
429+ - [ remove-duplicates-from-sorted-list] ( https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/ )
430+ - [ remove-duplicates-from-sorted-list-ii] ( https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/ )
431+ - [ reverse-linked-list] ( https://leetcode-cn.com/problems/reverse-linked-list/ )
432+ - [ reverse-linked-list-ii] ( https://leetcode-cn.com/problems/reverse-linked-list-ii/ )
433+ - [ merge-two-sorted-lists] ( https://leetcode-cn.com/problems/merge-two-sorted-lists/ )
434+ - [ partition-list] ( https://leetcode-cn.com/problems/partition-list/ )
435+ - [ sort-list] ( https://leetcode-cn.com/problems/sort-list/ )
436+ - [ reorder-list] ( https://leetcode-cn.com/problems/reorder-list/ )
437+ - [ linked-list-cycle] ( https://leetcode-cn.com/problems/linked-list-cycle/ )
438+ - [ linked-list-cycle-ii] ( https://leetcode-cn.com/problems/linked-list-cycle-ii/ )
439+ - [ palindrome-linked-list] ( https://leetcode-cn.com/problems/palindrome-linked-list/ )
440+ - [ copy-list-with-random-pointer] ( https://leetcode-cn.com/problems/copy-list-with-random-pointer/ )
0 commit comments