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; + } + +}