forked from xcv58/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add solutions for Intersection-of-Two-Linked-Lists
- Loading branch information
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <stdio.h> | ||
|
||
struct ListNode { | ||
int val; | ||
ListNode *next; | ||
ListNode(int x) : val(x), next(NULL) {} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { | ||
ListNode *a = headA, *b = headB, *lastA = a, *lastB = b; | ||
for (; a != NULL && b != NULL; lastA = a, lastB = b, a = a->next, b = b->next); | ||
for (; b != NULL; lastB = b, b = b->next, headB = headB->next); | ||
for (; a != NULL; lastA = a, a = a->next, headA = headA->next); | ||
for (; headA != NULL && headB != NULL && headA != headB; headA = headA->next, headB = headB->next); | ||
return (lastA != lastB) ? NULL :headA; | ||
} | ||
}; | ||
|
||
int main(int argc, char *argv[]) { | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
public class Solution { | ||
|
||
public class ListNode { | ||
int val; | ||
ListNode next; | ||
ListNode(int x) { | ||
val = x; | ||
next = null; | ||
} | ||
} | ||
|
||
public ListNode getIntersectionNode(ListNode headA, ListNode headB) { | ||
ListNode a = headA, b = headB, lastA = a, lastB = b; | ||
for (; a != null && b != null; lastA = a, lastB = b, a = a.next, b = b.next); | ||
for (; b != null; lastB = b, b = b.next, headB = headB.next); | ||
for (; a != null; lastA = a, a = a.next, headA = headA.next); | ||
for (; headA != null && headB != null && headA != headB; headA = headA.next, headB = headB.next); | ||
return (lastA != lastB) ? null :headA; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.next = None | ||
|
||
class Solution: | ||
# @param two ListNodes | ||
# @return the intersected ListNode | ||
def getIntersectionNode(self, headA, headB): | ||
s = set() | ||
while headA is not None: | ||
s.add(headA) | ||
headA = headA.next | ||
while headB is not None: | ||
if headB in s: | ||
return headB | ||
headB = headB.next | ||
return None |