Repository navigation
Expand file tree
/
Copy pathproblem234.java
More file actions
82 lines (70 loc) · 2.19 KB
/
Copy pathproblem234.java
File metadata and controls
82 lines (70 loc) · 2.19 KB
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
//234. Palindrome Linked List
import java.util.*;
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
class Solution {
public boolean isPalindrome(ListNode head) {
ListNode temp = head;
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
if(count==1)
return true;
int mid = (count / 2) + 1;
Stack<Integer> stack = new Stack<>();
boolean fl = count % 2 == 0;
count = 0;
temp = head; // Reset temp to the start of the list
while (temp != null) {
count++;
if (!fl && mid == count ) { // if count is odd and middle number
continue;
}
if (count < mid) {
stack.push(temp.val);
}
else
{
if (stack.peek() == temp.val)
{
stack.pop();
}
if (stack.isEmpty() && temp.next==null)
return true;
if(temp.next==null && (!stack.isEmpty()))
return false;
}
temp = temp.next;
}
return true;
}
}
public class problem234 {
public static void main(String[] args) {
// Test case: Convert array to linked list
int[] array = {1, 2,2,1}; // Example array
ListNode head = arrayToList(array);
// Create solution object and test the isPalindrome function
Solution solution = new Solution();
boolean result = solution.isPalindrome(head);
System.out.println("Is Palindrome: " + result); // Expected output: true
}
// Function to convert array to linked list
public static ListNode arrayToList(int[] array) {
if (array.length == 0) return null;
ListNode head = new ListNode(array[0]);
ListNode current = head;
for (int i = 1; i < array.length; i++) {
current.next = new ListNode(array[i]);
current = current.next;
}
return head;
}
}