Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Intersection of Two Linked Lists problem solution

YASH PAL, 31 July 2024

In this Leetcode Intersection of Two Linked Lists problem solution we have Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.

Leetcode Intersection of Two Linked Lists problem solution

Problem solution in Python.

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        s1=[]
        s2=[]
        
        while headA is not None:  
            s1.append(headA)
            headA=headA.next   
            
        while headB is not None:
            s2.append(headB)
            headB=headB.next
            
        wow = None
        while s1 and s2:
            
            if s1[-1] == s2.pop():
                wow=s1.pop()
            else:
                return wow
        return wow

Problem solution in Java.

public class Solution {

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    int ACount =0;
    int BCount=0;
    ListNode A=headA;
    ListNode B=headB;
    while(A!=null){
        ACount++;
        A=A.next;
    }
    
     while(B!=null){
        BCount++;
        B=B.next;
    }
    
    int diff= Math.abs(ACount-BCount);
    A=headA;
    B=headB;
    if(ACount>BCount){
        while(diff>0){
            A=A.next;
            diff--;
        }
    }else{
        while(diff>0){
            B=B.next;
            diff--;
        }
    }
    
    while(A!=B){
        A=A.next;
        B=B.next;
    }
    
    return A;
}
}

Problem solution in C++.

ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode * a = headA, *b = headB;
        while(a != NULL && b != NULL) {
            if(a == b) return a;
            a = a->next;
            b = b->next;
            if(a == NULL && b == NULL) return NULL;
            if(a == NULL) a = headB;
            if(b == NULL) b = headA;
        }
        return NULL;
    }

Problem solution in C.

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    if(headA==NULL && headB==NULL)
        return NULL;
    int countA=0;
    int countB=0;
    struct ListNode* tmpA=headA;
    struct ListNode* tmpB=headB;
    while(tmpA!=NULL || tmpB!=NULL){
        if(tmpA!=NULL){countA++;tmpA=tmpA->next;}
        if(tmpB!=NULL){countB++;tmpB=tmpB->next;}
    }
    tmpA=headA;
    tmpB=headB;
    int diff;

    if(countA>=countB){
        diff=countA-countB;
        while(diff){tmpA=tmpA->next;diff--;}

    }
    else{
        diff=countB-countA;
        while(diff){tmpB=tmpB->next;diff--;}
    }
    while(tmpA!=NULL && tmpB!=NULL){
        if(tmpA==tmpB)
            return tmpA;
        tmpA=tmpA->next;
        tmpB=tmpB->next;
    }
    return NULL;
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes