-
Notifications
You must be signed in to change notification settings - Fork 1
/
check-if-linked-list-is-pallindrome.java
46 lines (40 loc) · 1.4 KB
/
check-if-linked-list-is-pallindrome.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
class Solution {
// Function to check whether the list is palindrome.
boolean isPalindrome(Node head) {
if (head == null || head.next == null) return true;
// Step 1: Find the middle of the linked list
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
// Step 2: Reverse the second half of the list
Node secondHalfHead = reverseList(slow);
// Step 3: Compare the first half with the reversed second half
Node firstHalfHead = head;
Node secondHalfIter = secondHalfHead;
boolean isPalindrome = true;
while (secondHalfIter != null) {
if (firstHalfHead.data != secondHalfIter.data) {
isPalindrome = false;
break;
}
firstHalfHead = firstHalfHead.next;
secondHalfIter = secondHalfIter.next;
}
// Step 4: Restore the list (optional)
reverseList(secondHalfHead);
return isPalindrome;
}
// Helper function to reverse a linked list
private Node reverseList(Node head) {
Node prev = null, current = head, next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
}