-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentical_linked_list.java
106 lines (86 loc) · 2.13 KB
/
identical_linked_list.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//{ Driver Code Starts
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
class Node {
int data;
Node next;
public Node(int data){
this.data = data;
this.next = null;
}
}
class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
Node head1 = null, head2 = null, tail1 = null, tail2 = null;
//Input first LL
int n1 = sc.nextInt();
int d1 = sc.nextInt();
head1 = new Node(d1);
tail1 = head1;
while(n1-- > 1){
Node n = new Node(sc.nextInt());
tail1.next = n;
tail1 = tail1.next;
}
//Input second LL
int n2 = sc.nextInt();
int d2 = sc.nextInt();
head2 = new Node(d2);
tail2 = head2;
while(n2-- > 1){
Node n = new Node(sc.nextInt());
tail2.next = n;
tail2 = tail2.next;
}
Solution obj = new Solution();
if(obj.isIdentical(head1, head2))
System.out.println("Identical");
else
System.out.println("Not identical");
}
}
public static void po(Object o){
System.out.println(o);
}
public static void show(Node head){
while(head != null){
System.out.print(head.data+" ");
head = head.next;
}
System.out.println();
}
}
// } Driver Code Ends
/*
class Node {
int data;
Node next;
public Node(int data){
this.data = data;
this.next = null;
}
}*/
class Solution {
//Function to check whether two linked lists are identical or not.
public boolean isIdentical (Node head1, Node head2){
//write your code here
Node t1=head1;
Node t2=head2;
try{
while(t1!=null||t2!=null){
if(t1.data!=t2.data)
return false;
t1=t1.next;
t2=t2.next;
}
}
catch(Exception e){
return false;
}
return true;
}
}