File tree 2 files changed +44
-0
lines changed
2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change 167
167
290|[ Word Pattern] ( ./0290-word-pattern.js ) |Easy|
168
168
316|[ Remove Duplicate Letters] ( ./0316-remove-duplicate-letters.js ) |Medium|
169
169
326|[ Power of Three] ( ./0326-power-of-three.js ) |Easy|
170
+ 328|[ Odd Even Linked List] ( ./0328-odd-even-linked-list.js ) |Medium|
170
171
334|[ Increasing Triplet Subsequence] ( ./0334-increasing-triplet-subsequence.js ) |Medium|
171
172
342|[ Power of Four] ( ./0342-power-of-four.js ) |Easy|
172
173
344|[ Reverse String] ( ./0344-reverse-string.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 328. Odd Even Linked List
3
+ * https://leetcode.com/problems/odd-even-linked-list/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given the head of a singly linked list, group all the nodes with odd indices together followed
7
+ * by the nodes with even indices, and return the reordered list.
8
+ *
9
+ * The first node is considered odd, and the second node is even, and so on.
10
+ *
11
+ * Note that the relative order inside both the even and odd groups should remain as it was in
12
+ * the input.
13
+ *
14
+ * You must solve the problem in O(1) extra space complexity and O(n) time complexity.
15
+ */
16
+
17
+ /**
18
+ * Definition for singly-linked list.
19
+ * function ListNode(val, next) {
20
+ * this.val = (val===undefined ? 0 : val)
21
+ * this.next = (next===undefined ? null : next)
22
+ * }
23
+ */
24
+ /**
25
+ * @param {ListNode } head
26
+ * @return {ListNode }
27
+ */
28
+ var oddEvenList = function ( head ) {
29
+ if ( ! head ) return head ;
30
+
31
+ const even = head . next ;
32
+ let odd = head ;
33
+
34
+ while ( odd . next && odd . next . next ) {
35
+ const pointer = odd . next ;
36
+ odd . next = odd . next . next ;
37
+ odd = odd . next ;
38
+ pointer . next = odd . next ;
39
+ }
40
+
41
+ odd . next = even ;
42
+ return head ;
43
+ } ;
You can’t perform that action at this time.
0 commit comments