-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay57.java
More file actions
76 lines (66 loc) · 2.11 KB
/
Day57.java
File metadata and controls
76 lines (66 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class ListNode {
int val;
ListNode next;
ListNode child;
public ListNode(int val) {
this.val = val;
this.next = null;
this.child = null;
}
}
public class Day57 {
public ListNode flatten(ListNode head) {
if (head == null) return null;
ListNode current = head;
while (current != null) {
if (current.child != null) {
ListNode nextNode = current.next;
ListNode childList = flatten(current.child);
current.child = null;
current.next = childList;
while (current.next != null) {
current = current.next;
}
current.next = nextNode;
if (nextNode != null) {
nextNode = nextNode.next;
}
}
current = current.next;
}
return head;
}
public void printList(ListNode head) {
ListNode current = head;
while (current != null) {
System.out.print(current.val + " ");
current = current.next;
}
System.out.println();
}
public static void main(String[] args) {
FlattenLinkedList solution = new FlattenLinkedList();
// Create sample linked list
ListNode node1 = new ListNode(3);
ListNode node2 = new ListNode(3);
ListNode node3 = new ListNode(2);
ListNode node4 = new ListNode(1);
ListNode node5 = new ListNode(8);
ListNode node6 = new ListNode(10);
ListNode node7 = new ListNode(15);
ListNode node8 = new ListNode(18);
ListNode node9 = new ListNode(22);
ListNode node10 = new ListNode(29);
node1.child = node4;
node4.next = node3;
node3.next = node2;
node2.child = node5;
node5.next = node6;
node6.next = node7;
node7.child = node8;
node8.next = node9;
node9.next = node10;
ListNode flattenedList = solution.flatten(node1);
solution.printList(flattenedList);
}
}