-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeLinkedList.java
More file actions
59 lines (53 loc) · 1.47 KB
/
PalindromeLinkedList.java
File metadata and controls
59 lines (53 loc) · 1.47 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
package main.java.algorithm;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import main.java.algorithm.common.ListNode;
/**
* 234. Palindrome Linked List
* <p>
* Given the head of a singly linked list, return true if it is a palindrome.
* <p>
* Example 1:
* Input: head = [1,2,2,1]
* Output: true
* <p>
* Example 2:
* Input: head = [1,2]
* Output: false
* <p>
* Constraints:
* The number of nodes in the list is in the range [1, 10⁵].
* 0 <= Node.val <= 9
*/
public class PalindromeLinkedList
{
public static void main(String[] args)
{
ListNode head = new ListNode(1);
String palindrome = "12345678987654321";
char[] palindromeChar = palindrome.toCharArray();
for(int idx = 1; idx < palindromeChar.length; idx++) {
ListNode.addNodeValue(head, Integer.parseInt(String.valueOf(palindromeChar[idx])));
}
boolean isPal = isPalindrome(head);
System.out.println(isPal);
}
public static boolean isPalindrome(ListNode head) {
List<Integer> numbers = new ArrayList<>();
while(Objects.nonNull(head)) {
numbers.add(head.val);
head = head.next;
}
int init = 0;
int tail = numbers.size() -1;
while(init < tail) {
if(!Objects.equals(numbers.get(init), numbers.get(tail))) {
return false;
}
init++;
tail--;
}
return true;
}
}