Created
February 26, 2017 19:03
-
-
Save primaryobjects/53997c2eda7f81998e4b8578ccf1ffa3 to your computer and use it in GitHub Desktop.
Compare two linked lists. Compare values of 2 linked lists for equality.
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
int CompareLists(Node headA, Node headB) { | |
int result = 1; | |
Node current1 = headA; | |
Node current2 = headB; | |
while (current1 != null && current2 != null && current1.data == current2.data) { | |
current1 = current1.next; | |
current2 = current2.next; | |
} | |
if (current1 != null || current2 != null) { | |
// One of the lists has more items left in it. Otherwise, both lists are equal and fully traversed. | |
result = 0; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment