Skip to content

Commit c87e06a

Browse files
committedJan 13, 2025
Add solution #328
1 parent 9067fad commit c87e06a

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
290|[Word Pattern](./0290-word-pattern.js)|Easy|
168168
316|[Remove Duplicate Letters](./0316-remove-duplicate-letters.js)|Medium|
169169
326|[Power of Three](./0326-power-of-three.js)|Easy|
170+
328|[Odd Even Linked List](./0328-odd-even-linked-list.js)|Medium|
170171
334|[Increasing Triplet Subsequence](./0334-increasing-triplet-subsequence.js)|Medium|
171172
342|[Power of Four](./0342-power-of-four.js)|Easy|
172173
344|[Reverse String](./0344-reverse-string.js)|Easy|
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
};

0 commit comments

Comments
 (0)
Please sign in to comment.