Created
January 15, 2015 05:20
-
-
Save dmnugent80/f8f7e0339d570f6a7b4e to your computer and use it in GitHub Desktop.
Write an algorithm to determine if 2 linked lists intersect
This file contains hidden or 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
| public class Node{ | |
| int data; | |
| Node next; | |
| public Node(int d){ | |
| this.data = d. | |
| this.next = null; | |
| } | |
| } | |
| public boolean doLinkedListsIntersect(Node root1, Node root2){ | |
| Node curr1 = root1; | |
| Node curr2 = root2; | |
| while (curr1.next != null){ | |
| curr1 = curr1.next; | |
| } | |
| while (curr2.next != null){ | |
| curr2 = curr2.next; | |
| } | |
| if (curr1 == curr2){ | |
| return true; | |
| } | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment