Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Created February 26, 2017 19:03
Show Gist options
  • Save primaryobjects/53997c2eda7f81998e4b8578ccf1ffa3 to your computer and use it in GitHub Desktop.
Save primaryobjects/53997c2eda7f81998e4b8578ccf1ffa3 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