Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ListNode *detectLoop(ListNode *head){
- ListNode *slow=head,*fast=head;
- while(fast != NULL && fast->next != NULL){
- fast=fast->next->next;
- slow=slow->next;
- if(slow==fast)
- return slow;
- }
- return NULL;
- }
- ListNode* getIntersectionNode(ListNode *A, ListNode *B) {
- //make any one list as circular
- ListNode *p=A;
- while(p->next != NULL)
- p=p->next;
- ListNode *last=p;
- p->next=A;
- //Now A is circular and this problem is converted into
- //find loop in a LL
- ListNode *q=detectLoop(B);
- //p=detectLoop(B);
- if(!q) {
- last->next=NULL;
- return NULL;
- }
- p=B;
- while(p != q){
- p=p->next;
- q=q->next;
- }
- last->next=NULL;
- return p;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement