forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_234.java
103 lines (94 loc) · 2.8 KB
/
_234.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.fishercoder.solutions;
import com.fishercoder.common.classes.ListNode;
import java.util.ArrayList;
import java.util.List;
public class _234 {
public static class Solution1 {
/**
* O(n) time
* O(1) space
*/
public boolean isPalindrome(ListNode head) {
if (head == null) {
return true;
}
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
ListNode secondHalfHead = reverse(slow.next);
ListNode firstHalfHead = head;
while (firstHalfHead != null && secondHalfHead != null) {
if (firstHalfHead.val != secondHalfHead.val) {
return false;
}
firstHalfHead = firstHalfHead.next;
secondHalfHead = secondHalfHead.next;
}
return true;
}
private ListNode reverse(ListNode head) {
ListNode newHead = null;
while (head != null) {
ListNode next = head.next;
head.next = newHead;
newHead = head;
head = next;
}
return newHead;
}
}
public static class Solution2 {
/**
* O(n) time
* O(n) space
*/
public boolean isPalindrome(ListNode head) {
int len = 0;
ListNode fast = head;
ListNode slow = head;
List<Integer> firstHalf = new ArrayList<>();
while (fast != null && fast.next != null) {
fast = fast.next.next;
firstHalf.add(slow.val);
slow = slow.next;
len += 2;
}
if (fast != null) {
len++;
}
if (len % 2 != 0) {
slow = slow.next;
}
int i = firstHalf.size() - 1;
while (slow != null) {
if (firstHalf.get(i--) != slow.val) {
return false;
}
slow = slow.next;
}
return true;
}
}
public static class Solution3 {
/**
* O(n) time
* O(n) space
*/
public boolean isPalindrome(ListNode head) {
List<Integer> list = new ArrayList<>();
while (head != null) {
list.add(head.val);
head = head.next;
}
for (int i = 0, j = list.size() - 1; i <= j; i++, j--) {
if (list.get(i) != list.get(j)) {
return false;
}
}
return true;
}
}
}