Skip to content

Commit 920afb1

Browse files
first commit
1 parent ca08e80 commit 920afb1

File tree

3 files changed

+223
-0
lines changed

3 files changed

+223
-0
lines changed

Common/src/classes/ListNode.java

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package classes;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import utils.CommonUtils;
7+
8+
/**
9+
* Normally, both val and next should be private attributes and generate getter and setter for them,
10+
* but for the convenience of leetcode solutions, I set them as public.
11+
*/
12+
public class ListNode {
13+
public int val;
14+
public ListNode next;
15+
16+
public ListNode(int i) {
17+
this.val = i;
18+
}
19+
20+
public int val() {
21+
return val;
22+
}
23+
24+
public static ListNode createSinglyLinkedList() {
25+
ListNode head = new ListNode(1);
26+
27+
ListNode node1 = new ListNode(2);
28+
head.next = node1;
29+
30+
ListNode node2 = new ListNode(3);
31+
node1.next = node2;
32+
33+
ListNode node3 = new ListNode(4);
34+
node2.next = node3;
35+
36+
ListNode node4 = new ListNode(5);
37+
node3.next = node4;
38+
39+
ListNode node5 = new ListNode(6);
40+
node4.next = node5;
41+
42+
ListNode node6 = new ListNode(7);
43+
node5.next = node6;
44+
45+
ListNode node7 = new ListNode(8);
46+
node6.next = node7;
47+
48+
ListNode node8 = new ListNode(9);
49+
node7.next = node8;
50+
51+
ListNode node9 = new ListNode(10);
52+
node8.next = node9;
53+
return head;
54+
}
55+
56+
/**TODO: this function is NOT working as supposed to, I need to fix it!*/
57+
public static ListNode createSinglyLinkedList(List<Integer> listValues) {
58+
if (listValues == null || listValues.size() == 0)
59+
throw new IllegalArgumentException(
60+
"Please pass in a valid listValues to create a singly linked list.");
61+
ListNode head = new ListNode(listValues.get(0));
62+
for(int i : listValues){
63+
appendNode(head, i);
64+
}
65+
printList(head);
66+
return head;
67+
}
68+
69+
private static void appendNode(ListNode head, int i) {
70+
ListNode node = new ListNode(i);
71+
head.next = node;
72+
// head = head.next;
73+
}
74+
75+
public static void printList(ListNode head) {
76+
ListNode temp = head;
77+
System.out.println();
78+
while (temp != null) {
79+
System.out.print(temp.val() + "\t");
80+
temp = temp.next;
81+
}
82+
}
83+
84+
public static void main(String...strings){
85+
List<Integer> values = CommonUtils.randomIntArrayGenerator(10, 20);
86+
createSinglyLinkedList(values);
87+
ListNode head = createSinglyLinkedList();
88+
printList(head);
89+
System.out.println("The end.");
90+
}
91+
}

Common/src/utils/CommonUtils.java

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package utils;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Random;
6+
7+
import classes.ListNode;
8+
9+
public class CommonUtils {
10+
11+
private static final int DEFAULT_TREE_SIZE = 10;
12+
private static final int DEFAULT_UPPER_BOUND = 100;
13+
14+
public static void print(String message) {
15+
System.out.print(message);
16+
}
17+
18+
public static void print(int num) {
19+
System.out.print(num);
20+
}
21+
22+
public static void println(String message) {
23+
System.out.println(message);
24+
}
25+
26+
public static void println(int num) {
27+
System.out.println(num);
28+
}
29+
30+
public static void println() {
31+
System.out.println();
32+
}
33+
34+
//overloaded method to take only one argument
35+
public static List<Integer> randomIntArrayGenerator(int size) {
36+
return CommonUtils.randomIntArrayGenerator(size, DEFAULT_UPPER_BOUND);
37+
}
38+
39+
//overloaded method to take no argument
40+
public static List<Integer> randomIntArrayGenerator() {
41+
return CommonUtils.randomIntArrayGenerator(CommonUtils.DEFAULT_TREE_SIZE, DEFAULT_UPPER_BOUND);
42+
}
43+
44+
//this one has two other overloaded methods as above
45+
public static List<Integer> randomIntArrayGenerator(int size, int upperBound) {
46+
List<Integer> result = new ArrayList<Integer>();
47+
Random random = new Random();
48+
for (int i = 0; i < size; i++) {
49+
int randomInt = random.nextInt(upperBound);
50+
result.add(randomInt);
51+
print(String.valueOf(randomInt) + " ");
52+
}
53+
println();
54+
return result;
55+
}
56+
57+
//this one has two other overloaded methods as above
58+
public static List<Integer> uniqueIntArrayGenerator(int size) {
59+
List<Integer> result = new ArrayList<Integer>();
60+
for (int i = 0; i < size; i++) {
61+
result.add(i);
62+
}
63+
println();
64+
return result;
65+
}
66+
67+
// @Notes(context = "I'm assuing only classes in this PACKAGE will call the following two methods, so just leave the modifier as default, i.e. no public, private, or protected.")
68+
public
69+
static void printWhitespaces(int count) {
70+
for (int i = 0; i < count; i++)
71+
System.out.print(" ");
72+
}
73+
74+
public static <T> boolean isAllElementsNull(List<T> list) {
75+
for (Object object : list) {
76+
if (object != null)
77+
return false;
78+
}
79+
80+
return true;
81+
}
82+
83+
/**
84+
* If you want to print the reversed list out, you need to return the
85+
* reversed list's head, which was the end node of the original node. using
86+
* the following function.
87+
*/
88+
public static ListNode reverseList(ListNode head) {
89+
if (head == null || head.next == null) {
90+
return head;
91+
}
92+
ListNode previous, current, next;
93+
previous = head;
94+
current = head.next;
95+
while (current != null) {
96+
next = current.next;
97+
current.next = previous;
98+
previous = current;
99+
current = next;
100+
}
101+
head.next = null;
102+
return previous;
103+
}
104+
105+
public static void printList(ListNode head) {
106+
System.out.println("--------------------------------------------");
107+
while (head != null) {
108+
System.out.print(head.val);
109+
head = head.next;
110+
}
111+
System.out.println();
112+
}
113+
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package InterviewQuestions;
2+
3+
import classes.ListNode;
4+
5+
6+
/**Pinterest onsite question:
7+
* find the longest suffix in two linked lists
8+
* */
9+
public class FindLongestSuffixInTwoLists {
10+
11+
public ListNode findLongestSuffix(ListNode head1, ListNode head2){
12+
13+
}
14+
15+
public static void main(String...strings){
16+
17+
}
18+
}

0 commit comments

Comments
 (0)