Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

160. intersectionOfTwoLinkedLists.cpp #186

Open
PYQ0423 opened this issue Aug 30, 2019 · 2 comments
Open

160. intersectionOfTwoLinkedLists.cpp #186

PYQ0423 opened this issue Aug 30, 2019 · 2 comments

Comments

@PYQ0423
Copy link

PYQ0423 commented Aug 30, 2019

There is no swap method included, while you use swap(headA, headB).

@Gurpreet-Saini
Copy link

public class Solution {
    public ListNode getIntersectionNode(ListNode head1, ListNode head2) {
        int c1=getCount(head1);
        int c2=getCount(head2);
        
        if(c1>c2){
            int d=c1-c2;
            return getShift(d , head1 , head2);
        }else{
            int d= c2-c1;
            return getShift(d, head2, head1);
        }
        
    }
   public static int getCount(ListNode head){
       int count=0;
       while(head!=null){
           count++;
           head=head.next;
       }
       return count;
   }
    
    public static ListNode getShift(int d, ListNode head1 , ListNode head2){
       
        ListNode curr1=head1, curr2=head2;
        //head1 is greater
        for( int i =0;i< d; i++ ){
            curr1=curr1.next;
        }
        
        while(curr1!=null){
            if(curr1==curr2){
                return curr1;
            }
            curr1=curr1.next;
            curr2=curr2.next;
        }
        return null;
    }
}

@Debmalya017Das
Copy link

class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *temp=headA;
ListNode *temp2=headB;
while(temp!=temp2){
if(temp==NULL)
temp=headB;
else
temp=temp->next;
if(temp2==NULL)
temp2=headA;
else
temp2=temp2->next;

    }return temp;
}

};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants