Skip to content

Commit 347aac3

Browse files
find longest suffix in two linked lists (Pinterest onsite)
1 parent 920afb1 commit 347aac3

File tree

2 files changed

+86
-14
lines changed

2 files changed

+86
-14
lines changed

Common/src/utils/CommonUtils.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,13 @@ public static ListNode reverseList(ListNode head) {
102102
return previous;
103103
}
104104

105-
public static void printList(ListNode head) {
105+
public static void printList(final ListNode head) {
106+
ListNode temp = head;
106107
System.out.println("--------------------------------------------");
107-
while (head != null) {
108-
System.out.print(head.val);
109-
head = head.next;
108+
while (temp != null) {
109+
System.out.print(temp.val);
110+
temp = temp.next;
111+
if(temp != null) System.out.print("->");
110112
}
111113
System.out.println();
112114
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,88 @@
11
package InterviewQuestions;
22

3+
import utils.CommonUtils;
34
import classes.ListNode;
45

5-
6-
/**Pinterest onsite question:
7-
* find the longest suffix in two linked lists
6+
/**
7+
* Pinterest onsite question: find the longest suffix in two linked lists
8+
* e.g.
9+
* head1: a->b->d->g
10+
* head2: z->d->g
11+
* return head: d->g
812
* */
913
public class FindLongestSuffixInTwoLists {
1014

11-
public ListNode findLongestSuffix(ListNode head1, ListNode head2){
12-
13-
}
14-
15-
public static void main(String...strings){
16-
17-
}
15+
/**Idea: loop through two lists, find the length of each list,
16+
* then for the longer list, set the pointer to the place where both lists have the
17+
* same start point.
18+
* then start to check, when two nodes are the same, keep going, when they differ, restart.*/
19+
public ListNode findLongestSuffix(ListNode head1, ListNode head2) {
20+
ListNode p1 = head1;
21+
int len1 = 0;
22+
ListNode p2 = head2;
23+
int len2 = 0;
24+
25+
while(p1 != null){
26+
len1++;
27+
p1 = p1.next;
28+
}
29+
30+
while(p2 != null){
31+
len2++;
32+
p2 = p2.next;
33+
}
34+
35+
ListNode longList = (len1 > len2) ? head1 : head2;
36+
ListNode shortList = (longList == head1) ? head2 : head1;
37+
38+
System.out.println("len1 = " + len1 + "\tlen2 = " + len2);
39+
40+
//move the longList forward delta steps so that the two lists start from the same position
41+
int delta = Math.abs(len1 - len2);
42+
while(delta-- > 0){
43+
longList = longList.next;
44+
}
45+
CommonUtils.printList(longList);
46+
CommonUtils.printList(shortList);
47+
48+
ListNode sameSuffixHead = null;
49+
boolean newSuffixHeadFound = true;
50+
while(longList != null){
51+
if(longList.val == shortList.val){
52+
if(newSuffixHeadFound){
53+
sameSuffixHead = longList;
54+
newSuffixHeadFound = false;
55+
}
56+
} else {
57+
newSuffixHeadFound = true;
58+
}
59+
longList = longList.next;
60+
shortList = shortList.next;
61+
}
62+
63+
return sameSuffixHead;
64+
}
65+
66+
public static void main(String... strings) {
67+
ListNode head1 = new ListNode(1);
68+
head1.next = new ListNode(2);
69+
head1.next.next = new ListNode(3);
70+
head1.next.next.next = new ListNode(5);
71+
head1.next.next.next.next = new ListNode(4);
72+
CommonUtils.printList(head1);
73+
74+
ListNode head2 = new ListNode(9);
75+
head2.next = new ListNode(3);
76+
head2.next.next = new ListNode(8);
77+
head2.next.next.next = new ListNode(7);
78+
head2.next.next.next.next = new ListNode(5);
79+
head2.next.next.next.next.next = new ListNode(4);
80+
head2.next.next.next.next.next.next = new ListNode(5);
81+
head2.next.next.next.next.next.next.next = new ListNode(4);
82+
CommonUtils.printList(head2);
83+
84+
FindLongestSuffixInTwoLists test = new FindLongestSuffixInTwoLists();
85+
ListNode result = test.findLongestSuffix(head1, head2);
86+
CommonUtils.printList(result);
87+
}
1888
}

0 commit comments

Comments
 (0)