-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession-09-26-2019-problem-234-palindrome-linked-list.java
59 lines (51 loc) · 1.52 KB
/
session-09-26-2019-problem-234-palindrome-linked-list.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
/**
* Question: 234. Palindrome Linked List
* Link: https://leetcode.com/problems/palindrome-linked-list
* Hoplite Session: 09/26/2019
*
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* Approach: Find the middle. Reverse the other half (from middle element).
* Compare both the halves. If both are equal then its a palindrome.
*
*/
class Solution {
public boolean isPalindrome(ListNode head) {
ListNode fast = head, slow = head;
/**
* We find the middle using slow-fast pointer approach. Fast pointer moves 2
* steps and slow moves 1 when fast reaches the end. Slow will be in the middle.
*/
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
// If there are odd number of nodes shift middle
if (fast != null) {
slow = slow.next;
}
// Reverse elements from the middle node
ListNode mid = reverse(slow);
// Compare both the partition
while (mid != null) {
if (head.val != mid.val) {
return false;
}
head = head.next;
mid = mid.next;
}
return true;
}
// Reverses the Linkedlist
public ListNode reverse(ListNode head) {
ListNode prev = null;
while (head != null) {
ListNode next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
}