-
Notifications
You must be signed in to change notification settings - Fork 396
/
Copy pathpalindromeLinkedList.java
113 lines (85 loc) · 2.93 KB
/
palindromeLinkedList.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
104
105
106
107
108
109
110
111
112
113
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
SOLUTION 1 - Copy into Arraylist, then 2 pointer method!!
TC: O(N) to go through the entire list
SC: O(N) for the arraylist space
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null) return true;
ArrayList<Integer> list = new ArrayList<>();
ListNode curr = head;
while(curr!=null){
list.add(curr.val);
curr = curr.next;
}
int left = 0;
int right = list.size()-1;
while(left < right){
if(!list.get(left).equals(list.get(right))){ //need .equals since we are comparing Integers s
return false;
}
left++;
right--;
}
return true;
}
}
SOLUTION 2 - Reverse the list in place, ONLY IF WE ALLOWED TO MODIFY INPUT
Only issue is that, if we do this, in a concurrent environment (multiple threads and processes),
access to the linkedlist would hve to be locked while running this function, to avoid
issues of the linkedlist being modifyed
TC: O(N) - to go through the entire list
SC: O(1) - constant space, we modify the input
1.Find the end of the first half.
2. Reverse the second half.
3. Determine whether or not there is a palindrome.
4. Restore the list. //not necessary for input/output, but good programming practice
5. Return the result.
class Solution {
public boolean isPalindrome(ListNode head) {
if (head == null) return true;
// Find the end of first half and reverse second half.
ListNode firstHalfEnd = endOfFirstHalf(head);
ListNode secondHalfStart = reverseList(firstHalfEnd.next);
// Check whether or not there is a palindrome.
ListNode p1 = head;
ListNode p2 = secondHalfStart;
boolean result = true;
while (result && p2 != null) {
if (p1.val != p2.val) result = false;
p1 = p1.next;
p2 = p2.next;
}
// Restore the list and return the result.
firstHalfEnd.next = reverseList(secondHalfStart);
return result;
}
// Taken from https://leetcode.com/problems/reverse-linked-list/solution/
private ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
private ListNode endOfFirstHalf(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
}