Skip to content

Commit 8f5956b

Browse files
authored
Added task 2074.
1 parent ed0e722 commit 8f5956b

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package g2001_2100.s2074_reverse_nodes_in_even_length_groups;
2+
3+
// #Medium #Linked_List #2022_05_29_Time_9_ms_(62.36%)_Space_270.4_MB_(37.64%)
4+
5+
import com_github_leetcode.ListNode;
6+
7+
/*
8+
* Definition for singly-linked list.
9+
* public class ListNode {
10+
* int val;
11+
* ListNode next;
12+
* ListNode() {}
13+
* ListNode(int val) { this.val = val; }
14+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
15+
* }
16+
*/
17+
public class Solution {
18+
public ListNode reverseEvenLengthGroups(ListNode head) {
19+
int totalSize = size(head);
20+
int curSize = 1;
21+
ListNode dummy = new ListNode(-1);
22+
dummy.next = head;
23+
ListNode cur = dummy;
24+
while (totalSize > 0) {
25+
if (curSize % 2 == 0) {
26+
ListNode[] arr = reverse(cur.next, curSize);
27+
cur.next = arr[0];
28+
arr[1].next = arr[2];
29+
cur = arr[1];
30+
} else {
31+
for (int i = 0; i < curSize; i++) {
32+
cur = cur.next;
33+
}
34+
}
35+
totalSize -= curSize;
36+
curSize = totalSize >= curSize + 1 ? curSize + 1 : totalSize;
37+
}
38+
return head;
39+
}
40+
41+
private ListNode[] reverse(ListNode head, int size) {
42+
ListNode prev = null;
43+
ListNode forward = null;
44+
ListNode cur = head;
45+
while (size-- > 0) {
46+
forward = cur.next;
47+
cur.next = prev;
48+
prev = cur;
49+
cur = forward;
50+
}
51+
ListNode[] arr = new ListNode[3];
52+
arr[0] = prev;
53+
arr[1] = head;
54+
arr[2] = cur;
55+
return arr;
56+
}
57+
58+
private int size(ListNode head) {
59+
int size = 0;
60+
while (head != null) {
61+
size++;
62+
head = head.next;
63+
}
64+
return size;
65+
}
66+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2074\. Reverse Nodes in Even Length Groups
2+
3+
Medium
4+
5+
You are given the `head` of a linked list.
6+
7+
The nodes in the linked list are **sequentially** assigned to **non-empty** groups whose lengths form the sequence of the natural numbers (`1, 2, 3, 4, ...`). The **length** of a group is the number of nodes assigned to it. In other words,
8+
9+
* The <code>1<sup>st</sup></code> node is assigned to the first group.
10+
* The <code>2<sup>nd</sup></code> and the <code>3<sup>rd</sup></code> nodes are assigned to the second group.
11+
* The <code>4<sup>th</sup></code>, <code>5<sup>th</sup></code>, and <code>6<sup>th</sup></code> nodes are assigned to the third group, and so on.
12+
13+
Note that the length of the last group may be less than or equal to `1 + the length of the second to last group`.
14+
15+
**Reverse** the nodes in each group with an **even** length, and return _the_ `head` _of the modified linked list_.
16+
17+
**Example 1:**
18+
19+
![](https://assets.leetcode.com/uploads/2021/10/25/eg1.png)
20+
21+
**Input:** head = [5,2,6,3,9,1,7,3,8,4]
22+
23+
**Output:** [5,6,2,3,9,1,4,8,3,7]
24+
25+
**Explanation:**
26+
27+
- The length of the first group is 1, which is odd, hence no reversal occurs.
28+
29+
- The length of the second group is 2, which is even, hence the nodes are reversed.
30+
31+
- The length of the third group is 3, which is odd, hence no reversal occurs.
32+
33+
- The length of the last group is 4, which is even, hence the nodes are reversed.
34+
35+
**Example 2:**
36+
37+
![](https://assets.leetcode.com/uploads/2021/10/25/eg2.png)
38+
39+
**Input:** head = [1,1,0,6]
40+
41+
**Output:** [1,0,1,6]
42+
43+
**Explanation:**
44+
45+
- The length of the first group is 1. No reversal occurs.
46+
47+
- The length of the second group is 2. The nodes are reversed.
48+
49+
- The length of the last group is 1. No reversal occurs.
50+
51+
**Example 3:**
52+
53+
![](https://assets.leetcode.com/uploads/2021/11/17/ex3.png)
54+
55+
**Input:** head = [1,1,0,6,5]
56+
57+
**Output:** [1,0,1,5,6]
58+
59+
**Explanation:**
60+
61+
- The length of the first group is 1. No reversal occurs.
62+
63+
- The length of the second group is 2. The nodes are reversed.
64+
65+
- The length of the last group is 2. The nodes are reversed.
66+
67+
**Constraints:**
68+
69+
* The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.
70+
* <code>0 <= Node.val <= 10<sup>5</sup></code>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g2001_2100.s2074_reverse_nodes_in_even_length_groups;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.LinkedListUtils;
7+
import com_github_leetcode.ListNode;
8+
import org.junit.jupiter.api.Test;
9+
10+
class SolutionTest {
11+
@Test
12+
void reverseEvenLengthGroups() {
13+
ListNode head =
14+
LinkedListUtils.contructLinkedList(new int[] {5, 2, 6, 3, 9, 1, 7, 3, 8, 4});
15+
assertThat(
16+
new Solution().reverseEvenLengthGroups(head).toString(),
17+
equalTo("5, 6, 2, 3, 9, 1, 4, 8, 3, 7"));
18+
}
19+
20+
@Test
21+
void reverseEvenLengthGroups2() {
22+
ListNode head = LinkedListUtils.contructLinkedList(new int[] {1, 1, 0, 6});
23+
assertThat(new Solution().reverseEvenLengthGroups(head).toString(), equalTo("1, 0, 1, 6"));
24+
}
25+
26+
@Test
27+
void reverseEvenLengthGroups3() {
28+
ListNode head = LinkedListUtils.contructLinkedList(new int[] {1, 1, 0, 6, 5});
29+
assertThat(
30+
new Solution().reverseEvenLengthGroups(head).toString(), equalTo("1, 0, 1, 5, 6"));
31+
}
32+
}

0 commit comments

Comments
 (0)