-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPerson.java
More file actions
32 lines (27 loc) · 850 Bytes
/
Person.java
File metadata and controls
32 lines (27 loc) · 850 Bytes
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
import java.util.Arrays;
import java.util.List;
public class Person {
public String name;
public Person child;
public Person(String name) {
this.name = name;
this.child = null;
}
public static void main(String[] args) {
Person sarah = new Person("Sarah");
Person bob = new Person("Bob");
Person greg = new Person("greg");
List<Person> personList = Arrays.asList(sarah, bob, greg);
Person flatten = Person.flatten(personList);
}
public static Person flatten(List<Person> personList) {
if (personList == null || personList.size() == 0) return null;
if (personList.size() == 1) return personList.get(0);
for (int i =1; i<personList.size(); i++) {
Person curr = personList.get(i-1);
Person next = personList.get(i);
curr.child = next;
}
return personList.get(0);
}
}