-
Notifications
You must be signed in to change notification settings - Fork 94
/
Palindrome.java
76 lines (72 loc) · 1.61 KB
/
Palindrome.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
package chapter02LinkedList;
import java.util.Stack;
/**
*
* Problem: Given a singly linked list, determine if it is a palindrome.
*
* Follow up: Could you do it in O(n) time and O(1) space?
*
*/
public class Palindrome {
/**
* method 1: O(1) Space. The original LinkedList is changed
*/
public boolean isPalindrome1(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
// odd number nodes, ignore the middle node
if (fast != null) {
slow = slow.next;
}
// reverse the second part
ListNode headSecondPart = reverseList(slow);
while (headSecondPart != null) {
if (head.val != headSecondPart.val) {
return false;
}
head = head.next;
headSecondPart = headSecondPart.next;
}
return true;
}
// reverse LinkedList
public ListNode reverseList(ListNode head) {
ListNode newHead = null;
while (head != null) {
ListNode next = head.next;
head.next = newHead;
newHead = head;
head = next;
}
return newHead;
}
/**
* method 2: Stack, keep original LinkedList
*/
public boolean isPalindrome2(ListNode head) {
ListNode slow = head;
ListNode fast = head;
Stack<Integer> stack = new Stack<>();
while (fast != null && fast.next != null) {
stack.push(slow.val);
slow = slow.next;
fast = fast.next.next;
}
// odd number nodes, ignore the middle node
if (fast != null) {
slow = slow.next;
}
while (slow != null) {
int top = stack.pop().intValue();
if (top != slow.val) {
return false;
}
slow = slow.next;
}
return true;
}
}