Skip to content

Commit e640b43

Browse files
committed
link
1 parent 8e7a6db commit e640b43

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

ListNodeDemo.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public static void main(String[] args) {
5757
ListNode c1 = new ListNode(1);
5858
ListNode c2 = new ListNode(12);
5959
c1.next = c2;
60-
//c2.next = n1;
60+
c2.next = n1;
61+
//c2.next = c1;
6162

6263
ListNode mergeNode = mergeLink(m1, c1);
6364
//ListNode mergeNode2 = mergeLink(m1, c1);
@@ -114,6 +115,7 @@ public static void main(String[] args) {
114115

115116
System.out.println(isIntersect(n1, c1));
116117

118+
System.out.println("TEST the getFirstCommonNode:");
117119
ListNode cross = getFirstCommonNode(n1, c1);
118120
if (cross == null) {
119121
System.out.println("null");
@@ -650,10 +652,12 @@ public static ListNode getFirstCommonNode(ListNode head1, ListNode head2) {
650652
}
651653
}
652654

653-
while (head1 != null) {
654-
if (head1.next == head2.next) {
655-
return head1.next;
655+
while (head1 != null && head2 != null) {
656+
if (head1 == head2) {
657+
return head1;
656658
}
659+
head1 = head1.next;
660+
head2 = head2.next;
657661
}
658662

659663
return null;

0 commit comments

Comments
 (0)