-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListPalindrome.java
More file actions
46 lines (40 loc) · 1003 Bytes
/
Copy pathListPalindrome.java
File metadata and controls
46 lines (40 loc) · 1003 Bytes
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
class Solution {
public boolean isPalindrome(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while(fast!=null && fast.next!=null)
{
fast = fast.next.next;
slow = slow.next;
}
if(fast != null)
{
slow = slow.next;
}
slow = reverse(slow);
fast = head;
while( slow != null)
{
if(slow.val != fast.val)
return false;
else
slow = slow.next;
fast = fast.next;
}
return true;
}
public static ListNode reverse(ListNode head)
{
ListNode current = head;
ListNode prev = null;
while(current!=null)
{
ListNode temp = current.next;
current.next = prev;
prev = current;
current = temp;
}
return prev;
}
}
/*lastseen: 01/02/2019 */