Forked from primaryobjects/linked-list-compare.java
Created
September 4, 2021 01:41
-
-
Save jyshnkr/03e045c98d92d070cc412cef522c4b0f 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