Code Monkey home page Code Monkey logo

phpdbframework's Introduction

Leetcode - 160 Intersection of Two Linked Lists Using Two Pointer:

We can use TwoPointerTwo PointerTwoPointer algorithm to find intersected node.

Mathematics:

Consider the following Linked List(LL) Structure:

A-->B-->C-->D--|

...............|-->E-->F-->G

.......H-->L-->|

Here, head of first LL is A and head of second LL is H Both intersect at node E

Let distance from node A to D is x and distance from node H to L is y. Distance from node E to G is common and it is z.

During first traversal of LL1 and LL2, they will cover distance: for LL1 pointer --> x+z for LL2 pointer --> y+z

When the pointers inerchange their position in second traversal then distance covered till D and L are same : for LL1 pointer --> x+z+y for LL2 pointer --> y+z+x

if they will intersect then there exist a node that is common.

Complexity

  • Time Complexity: O(n+m)O(n+m)O(n+m)
  • Space Complexity: O(1)O(1)O(1)

Code: Java

 public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    ListNode node1 = headA, node2 = headB;
    int count = 0; // maintain count of traversal
    while(count<2){
        if(node1==node2) return node1;
        if(node1.next==null) ++count; // if no match after traversing twice then LL will not intersect.
        node1 = node1.next==null?headB:node1.next;
        node2 = node2.next==null?headA:node2.next;
    }

    return null;
}

phpdbframework's People

Contributors

koushikghosh11 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.