Skip to content

Commit a0c5189

Browse files
Sean PrashadSean Prashad
authored andcommitted
Update 116_Populating_Next_Right_Pointers_in_Each_Node.java
1 parent 8d0a057 commit a0c5189

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
class Solution {
22
public Node connect(Node root) {
3-
Node head = root, prev = root;
3+
if (root == null) {
4+
return null;
5+
}
46

5-
while (prev != null) {
6-
Node curr = prev;
7+
Node leftMostNode = root;
78

8-
while (curr != null) {
9-
if (curr.left != null) {
10-
curr.left.next = curr.right;
11-
}
9+
while (leftMostNode.left != null) {
10+
Node head = leftMostNode;
11+
12+
while (head != null) {
13+
head.left.next = head.right;
1214

13-
if (curr.right != null && curr.next != null) {
14-
curr.right.next = curr.next.left;
15+
if (head.next != null) {
16+
head.right.next = head.next.left;
1517
}
1618

17-
curr = curr.next;
19+
head = head.next;
1820
}
1921

20-
prev = prev.left;
22+
leftMostNode = leftMostNode.left;
2123
}
2224

23-
return head;
25+
return root;
2426
}
2527
}

0 commit comments

Comments
 (0)