package sort;
import java.util.Stack;
public class Test52 {
public static void main(String[] args) {
ListNode a = new ListNode(1);
ListNode b = new ListNode(2);
ListNode c = new ListNode(3);
ListNode d = new ListNode(4);
ListNode e = new ListNode(5);
ListNode f = new ListNode(6);
ListNode g = new ListNode(7);
// 第一个List
a.next = b;
b.next = c;
c.next = f;
f.next = g;
// 第二个List
d.next = e;
e.next = f;
f.next = g;
System.out.println(function(a, d).date);
}
public static ListNode function(ListNode list1, ListNode list2) {
Stack<ListNode> stack1 = new Stack<ListNode>();
Stack<ListNode> stack2 = new Stack<ListNode>();
while (list1 != null) {
stack1.push(list1);
list1 = list1.next;
}
while (list2 != null) {
stack2.push(list2);
list2 = list2.next;
}
while (!stack1.isEmpty() && !stack2.isEmpty()
&& stack1.peek() == stack2.peek()) {
stack1.pop();
stack2.pop();
}
return stack1.peek().next;
}
public static ListNode function2(ListNode list1, ListNode list2) {
int length1 = 0;
int length2 = 0;
int cha = 0;
ListNode temp = list1;
while (temp != null) {
length1++;
temp = temp.next;
}
temp = list2;
while (temp != null) {
length2++;
temp = temp.next;
}
if (length1 > length2) {
cha = length1 - length2;
for (int i = 0; i < cha; i++) {
list1 = list1.next;
}
while (list1 != null && list2 != null && list1 != list2) {
list1 = list1.next;
list2 = list2.next;
}
} else {
cha = length2 - length1;
for (int i = 0; i < cha; i++) {
list2 = list2.next;
}
while (list1 != null && list2 != null && list1 != list2) {
list1 = list1.next;
list2 = list2.next;
}
}
return list2;
}
}