-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
61 lines (55 loc) · 1.38 KB
/
Solution.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
package palindromelinkedlist;
import datastructures.ListNode;
import java.util.ArrayList;
/**
* Given a singly linked list, determine if it is a palindrome.
*
* Example 1:
*
* Input: 1->2
* Output: false
* Example 2:
*
* Input: 1->2->2->1
* Output: true
* Follow up:
* Could you do it in O(n) time and O(1) space?
*/
class Solution {
/**
* Runtime: 3 ms, faster than 30.74% of Java online submissions for Palindrome Linked List.
* Memory Usage: 43.1 MB, less than 53.13% of Java online submissions for Palindrome Linked List.
*
* Time - O(n) | Space - O(n)
* @param head
* @return
*/
private boolean isPalindrome(ListNode head) {
ArrayList<Integer> nodes = new ArrayList<>();
ListNode curr = head;
while(curr != null) {
nodes.add(curr.val);
curr = curr.next;
}
curr = reverseList(head);
int i = 0;
while(curr != null) {
if(curr.val != nodes.get(i)) {
return false;
}
curr = curr.next;
i++;
}
return true;
}
private ListNode reverseList(ListNode head) {
ListNode prev = null, curr = head, temp = null;
while(curr != null) {
temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
return prev;
}
}