-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFindLongestSuffixInTwoLists.java
85 lines (82 loc) · 2.88 KB
/
FindLongestSuffixInTwoLists.java
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
77
78
79
80
81
82
83
84
85
package interviewQuestions;
/**
* Pinterest onsite question: find the longest suffix in two linked lists
* e.g.
* head1: a->b->d->g
* head2: z->d->g
* return head: d->g
* */
public class FindLongestSuffixInTwoLists {
/**Idea: loop through two lists, find the length of each list,
* then for the longer list, set the pointer to the place where both lists have the
* same start point.
* then start to check, when two nodes are the same, keep going, when they differ, restart.*/
// public ListNode findLongestSuffix(ListNode head1, ListNode head2) {
// ListNode p1 = head1;
// int len1 = 0;
// ListNode p2 = head2;
// int len2 = 0;
//
// while(p1 != null){
// len1++;
// p1 = p1.next;
// }
//
// while(p2 != null){
// len2++;
// p2 = p2.next;
// }
//
// ListNode longList = (len1 > len2) ? head1 : head2;
// ListNode shortList = (longList == head1) ? head2 : head1;
//
// System.out.println("len1 = " + len1 + "\tlen2 = " + len2);
//
// //move the longList forward delta steps so that the two lists start from the same position
// int delta = Math.abs(len1 - len2);
// while(delta-- > 0){
// longList = longList.next;
// }
// CommonUtils.printList(longList);
// CommonUtils.printList(shortList);
//
// ListNode sameSuffixHead = null;
// boolean newSuffixHeadFound = true;
// while(longList != null){
// if(longList.val == shortList.val){
// if(newSuffixHeadFound){
// sameSuffixHead = longList;
// newSuffixHeadFound = false;
// }
// } else {
// newSuffixHeadFound = true;
// }
// longList = longList.next;
// shortList = shortList.next;
// }
//
// return sameSuffixHead;
// }
public static void main(String... strings) {
// ListNode head1 = new ListNode(1);
// head1.next = new ListNode(2);
// head1.next.next = new ListNode(3);
// head1.next.next.next = new ListNode(5);
// head1.next.next.next.next = new ListNode(4);
// CommonUtils.printList(head1);
//
// ListNode head2 = new ListNode(9);
// head2.next = new ListNode(3);
// head2.next.next = new ListNode(8);
// head2.next.next.next = new ListNode(7);
// head2.next.next.next.next = new ListNode(5);
// head2.next.next.next.next.next = new ListNode(4);
// head2.next.next.next.next.next.next = new ListNode(5);
// head2.next.next.next.next.next.next.next = new ListNode(4);
// CommonUtils.printList(head2);
//
// FindLongestSuffixInTwoLists test = new FindLongestSuffixInTwoLists();
// ListNode result = test.findLongestSuffix(head1, head2);
// CommonUtils.printList(result);
}
}