Skip to content

Commit 06a9526

Browse files
committed
Create: 0160-intersection-of-two-linked-lists.java
1 parent e5730f0 commit 06a9526

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Diff for: java/0160-intersection-of-two-linked-lists.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
14+
ListNode a = headA, b = headB;
15+
while (a != b) {
16+
a = (a != null) ? a.next : headB;
17+
b = (b != null) ? b.next : headA;
18+
}
19+
return a;
20+
}
21+
}

0 commit comments

Comments
 (0)