-
Notifications
You must be signed in to change notification settings - Fork 13
/
LL_LinkedListIsPalindrome.java
88 lines (75 loc) · 2.56 KB
/
LL_LinkedListIsPalindrome.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// 11.07.2020
// https://practice.geeksforgeeks.org/problems/check-if-linked-list-is-pallindrome/1/
// below are three very good video tutorials.
// https://www.youtube.com/watch?v=3thY0z3Pf5k
// https://www.youtube.com/watch?v=bOGh_3MTrdE
// https://www.youtube.com/watch?v=wk4QsvwQwdQ
public class LL_LinkedListIsPalindrome {
private static boolean isLinkedListPalindrome(Node head) {
if(head == null) return true;
Node midNode = getMidNode(head);
System.out.println(midNode.data);
Node firstHalfHeadNode = head;
Node secondHalfHeadNode = reverseLinkedList(midNode.next);
while (secondHalfHeadNode != null) {
if(firstHalfHeadNode.data != secondHalfHeadNode.data) {
return false;
}
firstHalfHeadNode = firstHalfHeadNode.next;
secondHalfHeadNode = secondHalfHeadNode.next;
}
return true;
}
private static Node reverseLinkedList(Node head) {
if(head == null) return null;
Node prev = null;
Node curr = head;
Node next = null;
while(curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
private static Node getMidNode(Node head) {
Node slow = head;
Node fast = head;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine().trim());
String[] str = br.readLine().trim().split("\\s+");
Node head = buildLinkedList(str);
System.out.println(isLinkedListPalindrome(head) ? 1 : 0);
}
}
private static Node buildLinkedList(String[] str) {
Node head = new Node(Integer.parseInt(str[0]));
Node currNode = head;
for (int i = 1; i < str.length; i++) {
currNode.next = new Node(Integer.parseInt(str[i]));
currNode = currNode.next;
}
return head;
}
static class Node {
private int data;
private Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
}