diff --git a/java/0160-intersection-of-two-linked-lists.java b/java/0160-intersection-of-two-linked-lists.java new file mode 100644 index 000000000..c5b1e7f98 --- /dev/null +++ b/java/0160-intersection-of-two-linked-lists.java @@ -0,0 +1,21 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } + * } + */ +public class Solution { + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + ListNode a = headA, b = headB; + while (a != b) { + a = (a != null) ? a.next : headB; + b = (b != null) ? b.next : headA; + } + return a; + } +} \ No newline at end of file