Programming/Data Structure
[Hackerrank] CompareTwoLinkedLists
읽고 쓰는 개발자
2020. 12. 2. 22:27
https://www.hackerrank.com/challenges/compare-two-linked-lists/problem
Compare two linked lists | HackerRank
Compare the data in two linked lists node by node to see if the lists contain identical data.
www.hackerrank.com
package exam.complete;
//https://www.hackerrank.com/challenges/compare-two-linked-lists/problem
import exam.SinglyLinkedListNode;
public class CompareTwoLinkedLists {
static boolean compareLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2) {
if(head1.data != head2.data) return false;
if(head1.next == null && head2.next == null) return true;
if(head1.next == null || head2.next == null) return false;
return compareLists(head1.next, head2.next);
}
}