Skip to content

Commit

Permalink
Merge pull request AdarshAddee#66 from fulcrum999/patch-1
Browse files Browse the repository at this point in the history
Created intersection-of-two-linked-lists.cpp
  • Loading branch information
AdarshAddee committed Oct 1, 2022
2 parents 11fd979 + a859520 commit bf9b227
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions C++/intersection-of-two-linked-lists.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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.

// Time: O(m + n)
// Space: O(1)

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/

class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *curA = headA, *curB = headB;
while (curA != curB) {
curA = curA ? curA->next : headB;
curB = curB ? curB->next : headA;
}
return curA;
}
};

0 comments on commit bf9b227

Please sign in to comment.