|
1 | 1 | package InterviewQuestions;
|
2 | 2 |
|
| 3 | +import utils.CommonUtils; |
3 | 4 | import classes.ListNode;
|
4 | 5 |
|
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 |
8 | 12 | * */
|
9 | 13 | public class FindLongestSuffixInTwoLists {
|
10 | 14 |
|
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 | + } |
18 | 88 | }
|
0 commit comments