題目:輸入兩個(gè)鏈表,找出它們的第一個(gè)公共結(jié)點(diǎn)主巍。
代碼如下:
package demo;
/**
* 兩個(gè)鏈表的第1個(gè)公共結(jié)點(diǎn)
*
* @author xiangdonglee
*
*/
public class Test37 {
private static class ListNode {
int val;
ListNode next;
public ListNode() {
}
public ListNode(int val) {
this.val = val;
}
@Override
public String toString() {
return val + "";
}
}
public static ListNode findFirstCommonNode(ListNode head1, ListNode head2) {
// 得到2個(gè)鏈表的長(zhǎng)度
int length1 = getListLength(head1);
int length2 = getListLength(head2);
int diff = length1 - length2;
ListNode longListHead = head1;
ListNode shortListHead = head2;
if (diff < 0) {
longListHead = head2;
shortListHead = head1;
diff = length2 - length1;
}
// 先在長(zhǎng)鏈表上走幾步笋敞,再同時(shí)在2個(gè)鏈表上遍歷
for (int i = 0; i < diff; i++) {
longListHead = longListHead.next;
}
while (longListHead != null && shortListHead != null && longListHead != shortListHead) {
longListHead = longListHead.next;
shortListHead = shortListHead.next;
}
// 返回第一個(gè)公共結(jié)點(diǎn)碱蒙,如果沒(méi)有就返回null
return longListHead;
}
private static int getListLength(ListNode head) {
int result = 0;
while (head != null) {
result++;
head = head.next;
}
return result;
}
public static void main(String[] args) {
test1();
test2();
test3();
test4();
test5();
}
/**
* 第1個(gè)公共結(jié)點(diǎn)在鏈表中間
*/
private static void test1() {
ListNode node1 = new ListNode(1);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(3);
ListNode node4 = new ListNode(4);
ListNode node5 = new ListNode(5);
ListNode node6 = new ListNode(6);
ListNode node7 = new ListNode(7);
node1.next = node2;
node2.next = node3;
node3.next = node6;
node6.next = node7;
node4.next = node5;
node5.next = node6;
System.out.println(findFirstCommonNode(node1, node4));
}
/**
* 沒(méi)有公共結(jié)點(diǎn)
*/
private static void test2() {
ListNode node1 = new ListNode(1);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(3);
ListNode node4 = new ListNode(4);
ListNode node5 = new ListNode(5);
ListNode node6 = new ListNode(6);
ListNode node7 = new ListNode(7);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node5.next = node6;
node6.next = node7;
System.out.println(findFirstCommonNode(node1, node5));
}
/**
* 公共結(jié)點(diǎn)是最后一個(gè)結(jié)點(diǎn)
*/
private static void test3() {
ListNode node1 = new ListNode(1);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(3);
ListNode node4 = new ListNode(4);
ListNode node5 = new ListNode(5);
ListNode node6 = new ListNode(6);
ListNode node7 = new ListNode(7);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node7;
node5.next = node6;
node6.next = node7;
System.out.println(findFirstCommonNode(node1, node5));
}
/**
* 公共結(jié)點(diǎn)是第一個(gè)結(jié)點(diǎn),2個(gè)鏈表完全重合
*/
private static void test4() {
ListNode node1 = new ListNode(1);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(3);
ListNode node4 = new ListNode(4);
ListNode node5 = new ListNode(5);
ListNode node6 = new ListNode(6);
ListNode node7 = new ListNode(7);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
node5.next = node6;
node6.next = node7;
System.out.println(findFirstCommonNode(node1, node1));
}
/**
* 輸入的鏈表頭結(jié)點(diǎn)是null指針
*/
private static void test5() {
System.out.println(findFirstCommonNode(null, null));
}
}
來(lái)源:http://blog.csdn.net/derrantcm/article/details/46761093