Skip to content

Commit

Permalink
Create 0160-intersection-of-two-linked-lists.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Ritesh7766 authored Apr 8, 2023
1 parent 69fe8c8 commit a1ca737
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cpp/0160-intersection-of-two-linked-lists.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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 *trevA = headA, *trevB = headB;

while (trevA != trevB) {
trevA = (trevA != NULL) ? trevA->next : headA;
trevB = (trevB != NULL) ? trevB->next : headB;
}
return trevA;
}
};

0 comments on commit a1ca737

Please sign in to comment.