File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * 143. Reorder List
3+ * https://leetcode.com/problems/reorder-list/
4+ * Difficulty: Medium
5+ *
6+ * You are given the head of a singly linked-list. The list can be represented as:
7+ *
8+ * L0 → L1 → … → Ln - 1 → Ln
9+ * Reorder the list to be on the following form:
10+ *
11+ * L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
12+ * You may not modify the values in the list's nodes. Only nodes themselves may be changed.
13+ */
14+
15+ /**
16+ * Definition for singly-linked list.
17+ * function ListNode(val, next) {
18+ * this.val = (val===undefined ? 0 : val)
19+ * this.next = (next===undefined ? null : next)
20+ * }
21+ */
22+ /**
23+ * @param {ListNode } head
24+ * @return {void } Do not return anything, modify head in-place instead.
25+ */
26+ var reorderList = function ( head ) {
27+ if ( ! head || ! head . next || ! head . next . next ) {
28+ return head ;
29+ }
30+
31+ let list1 = head ;
32+ let list2 = head ;
33+
34+ while ( list2 . next && list2 . next . next ) {
35+ list1 = list1 . next ;
36+ list2 = list2 . next . next ;
37+ }
38+
39+ let center1 = list1 ;
40+ let center2 = list1 . next ;
41+ while ( center2 . next ) {
42+ const temp = center2 . next ;
43+ center2 . next = temp . next ;
44+ temp . next = center1 . next ;
45+ center1 . next = temp ;
46+ }
47+
48+ list1 = head ;
49+ list2 = center1 . next ;
50+ while ( list1 != center1 ) {
51+ center1 . next = list2 . next ;
52+ list2 . next = list1 . next ;
53+ list1 . next = list2 ;
54+ list1 = list2 . next ;
55+ list2 = center1 . next ;
56+ }
57+ } ;
Original file line number Diff line number Diff line change 7777136|[ Single Number] ( ./0136-single-number.js ) |Easy|
7878141|[ Linked List Cycle] ( ./0141-linked-list-cycle.js ) |Easy|
7979142|[ Linked List Cycle II] ( ./0142-linked-list-cycle-ii.js ) |Medium|
80+ 143|[ Reorder List] ( ./0143-reorder-list.js ) |Medium|
8081144|[ Binary Tree Preorder Traversal] ( ./0144-binary-tree-preorder-traversal.js ) |Easy|
8182145|[ Binary Tree Postorder Traversal] ( ./0145-binary-tree-postorder-traversal.js ) |Easy|
8283151|[ Reverse Words in a String] ( ./0151-reverse-words-in-a-string.js ) |Medium|
You can’t perform that action at this time.
0 commit comments