We say two linked-lists are equal if they have the same number of elements, and the elements themselves are the same. Furthermore elements' order are not significant.
If compared linked-lists are not sorted, the straightforward approach to compare two linked-lists can be achieved in following way:
1)Define a method to determine whether a linked-list is a subset of the other. This can be achieved through enumerating every items in a list and determine whether they exist in the other list.
2)If a list is a subset of the other one, and vice versa, we can say that the two linked-lists are equal.
If compared linked-lists are sorted, comparing linked-lists become easier. I write the following program to sort linked-lists firstly, and then compare them. The sort algorithms can be categorized as inserting-sort.
class Node {
int value;
Node next;
Node(int v){
value=v;
next =null;
}
Node(){
this(0);
}
}
class LinkListHandle {
static boolean equals(Node head1,Node head2){
while(head1!=null && head2!=null){
if(head1.value!=head2.value){
return false;
}
head1=head1.next;
head2=head2.next;
}
if(head1==head2){
return true;
}else{
return false;
}
}
static Node clone(Node head){
if(head==null) return null;
Node first=new Node(head.value);
Node remaining=clone(head.next);
first.next=remaining;
return first;
}
static Node seqInsert(Node head, Node elem){
elem.next=head;
return elem;
}
static Node sortInsert(Node head, Node elem){
Node cur=head;
Node prev=null;
while(cur!=null && elem.value>cur.value){
prev=cur;
cur=cur.next;
}
//insert to a empty list
if(cur==null && prev==null) {
elem.next=null;
return elem;
}
//insert at tail of the list
if(cur==null && prev!=null) {
elem.next=null;
prev.next=elem;
return head;
}
//insert at head of the list
if(cur!=null && prev==null){
elem.next=head;
return elem;
}
//insert at middle of the list
if(cur!=null && prev!=null){
prev.next=elem;
elem.next=cur;
return head;
}
//should not reach here
return null;
}
static Node insertSort(Node head){
if(head==null) return head;
Node newHead=null;
Node next=null;
while(head!=null){
//must remember next elem here, because sortInsert may change head's next field
next=head.next;
newHead=sortInsert(newHead,head);
head=next;
}
return newHead;
}
static void printLinkList(Node head){
System.out.print('[');
Node prev=null;
Node cur=head;
while(cur!=null){
if(prev!=null) {
System.out.print(prev.value);
System.out.print(',');
}
prev=cur;
cur=cur.next;
}
if(cur==null && prev!=null){
System.out.print(prev.value);
}
System.out.println(']');
}
}
public class JavaLearn {
public static void main(String[] args) {
// TODO, add your application code
Random rnd=new Random();
Node head1=null;
Node head2=null;
for(int i=0;i<10;i++){
head1=LinkListHandle.seqInsert(head1,new Node(rnd.nextInt() % 100));
}
head2=LinkListHandle.clone(head1);
System.out.println("head1");
LinkListHandle.printLinkList(head1);
System.out.println("head2");
LinkListHandle.printLinkList(head2);
head2=LinkListHandle.insertSort(head2);
System.out.println("head1");
LinkListHandle.printLinkList(head1);
System.out.println("head2 sort");
LinkListHandle.printLinkList(head2);
head1=LinkListHandle.insertSort(head1);
System.out.println("head1 sort");
LinkListHandle.printLinkList(head1);
System.out.println("head2 sort");
LinkListHandle.printLinkList(head2);
if(LinkListHandle.equals(head1,head2)){
System.out.println("they are equal");
}else{
System.out.println("they are not equal");
}
}
}