Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1461c5e

Browse files
author
Ram swaroop
committed
check identical lists: done
1 parent 439032f commit 1461c5e

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package me.ramswaroop.linkedlists;
2+
3+
import me.ramswaroop.common.SingleLinkedList;
4+
import me.ramswaroop.common.SingleLinkedNode;
5+
6+
/**
7+
* Created by IntelliJ IDEA.
8+
*
9+
* @author: ramswaroop
10+
* @date: 6/29/15
11+
* @time: 9:42 AM
12+
*/
13+
public class IsIdentical<E extends Comparable<E>> extends SingleLinkedList<E> {
14+
15+
/**
16+
* Returns {@code true} if linked list {@param list1} and {@param list2}
17+
* are identical i.e, the data in the nodes as well as their arrangements are
18+
* similar. Ex: 1->2->3 and 1->2->3 are identical.
19+
*
20+
* @param list1
21+
* @param list2
22+
* @param <E>
23+
* @return
24+
*/
25+
public static <E extends Comparable<E>> boolean isIdentical(SingleLinkedList<E> list1,
26+
SingleLinkedList<E> list2) {
27+
28+
// base cases
29+
if (list1.size != list2.size) {
30+
return false;
31+
} else if (list1.size == 0 && list2.size == 0) {
32+
return true;
33+
}
34+
35+
SingleLinkedNode<E> curr1 = list1.getNode(0), curr2 = list2.getNode(0);
36+
37+
while (curr1 != null && curr2 != null) {
38+
if (!curr1.item.equals(curr2.item)) {
39+
return false;
40+
}
41+
curr1 = curr1.next;
42+
curr2 = curr2.next;
43+
}
44+
45+
return true;
46+
}
47+
48+
public static void main(String a[]) {
49+
SingleLinkedList<Integer> linkedList1 = new SingleLinkedList<>();
50+
linkedList1.add(00);
51+
linkedList1.add(11);
52+
linkedList1.add(22);
53+
linkedList1.add(33);
54+
linkedList1.add(44);
55+
linkedList1.add(55);
56+
linkedList1.add(66);
57+
linkedList1.printList();
58+
SingleLinkedList<Integer> linkedList2 = new SingleLinkedList<>();
59+
linkedList2.add(00);
60+
linkedList2.add(11);
61+
linkedList2.add(22);
62+
linkedList2.add(33);
63+
linkedList2.add(44);
64+
linkedList2.add(55);
65+
linkedList2.add(66);
66+
linkedList2.printList();
67+
System.out.println(isIdentical(linkedList1, linkedList2));
68+
}
69+
}

0 commit comments

Comments
 (0)