Skip to content

Instantly share code, notes, and snippets.

@jyshnkr
Forked from primaryobjects/linked-list-compare.java
Created September 4, 2021 01:41
Show Gist options
  • Save jyshnkr/03e045c98d92d070cc412cef522c4b0f to your computer and use it in GitHub Desktop.
Save jyshnkr/03e045c98d92d070cc412cef522c4b0f to your computer and use it in GitHub Desktop.
Compare two linked lists. Compare values of 2 linked lists for equality.
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