-
Notifications
You must be signed in to change notification settings - Fork 20
/
PalindromeLinkedList.java
73 lines (62 loc) · 2.05 KB
/
PalindromeLinkedList.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
class ListNode {
int val;
ListNode next;
public ListNode(int val) {
this.val = val;
this.next = null;
}
}
public class PalindromeLinkedList {
public boolean isPalindrome(ListNode head) {
if (head == null || head.next == null) {
return true;
}
// Find the middle of the linked list
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
// Reverse the second half of the linked list
ListNode secondHalf = reverse(slow.next);
slow.next = null; // Break the original list from the middle
// Compare the first and reversed second half
ListNode p1 = head;
ListNode p2 = secondHalf;
while (p1 != null && p2 != null) {
if (p1.val != p2.val) {
return false;
}
p1 = p1.next;
p2 = p2.next;
}
// Reconstruct the original list if necessary
slow.next = reverse(secondHalf);
return true;
}
// Helper function to reverse a linked list
private ListNode reverse(ListNode head) {
ListNode prev = null;
ListNode current = head;
while (current != null) {
ListNode nextNode = current.next;
current.next = prev;
prev = current;
current = nextNode;
}
return prev;
}
// Example usage
public static void main(String[] args) {
PalindromeLinkedList solution = new PalindromeLinkedList();
// Create a sample linked list: 1 -> 2 -> 3 -> 2 -> 1
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(2);
head.next.next.next.next = new ListNode(1);
boolean isPalindrome = solution.isPalindrome(head);
System.out.println("Is the linked list a palindrome? " + isPalindrome); // Output: true
}
}