From b2eebf319783a5428f04462ccc088c03f7e0b952 Mon Sep 17 00:00:00 2001 From: gargee suresh Date: Thu, 1 Oct 2020 23:50:10 +0530 Subject: [PATCH] Create intersection-of-two-linked-lists.java code in java to find the intersection node of two linked lists from java --- .../intersection-of-two-linked-lists.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 linklist codes/intersection-of-two-linked-lists.java diff --git a/linklist codes/intersection-of-two-linked-lists.java b/linklist codes/intersection-of-two-linked-lists.java new file mode 100644 index 00000000..32714044 --- /dev/null +++ b/linklist codes/intersection-of-two-linked-lists.java @@ -0,0 +1,50 @@ +/** + * 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) + { + int a=len(headA); + int b=len(headB); + int c; + if(a0){ + headB=headB.next; + c--; + } + } + else{ + c=a-b; + while(c>0){ + headA=headA.next; + c--; + } + } + + while(headA!=headB && headA!=null){ + headA=headA.next; + headB=headB.next; + } + return headA; + } + + public int len(ListNode head){ + int a=0; + while(head!=null){ + a++; + head=head.next; + } + return a; + } + +}