-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo_234.cs
66 lines (57 loc) · 1.7 KB
/
No_234.cs
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
namespace LeetCode
{
public class No_234
{
/// <summary>
/// Runtime: 260ms Beats: 21.39%, Memory: 62.3mb Beats: 19.60%
/// </summary>
public bool IsPalindrome(ListNode head)
{
List<int> copy = new List<int>();
while (head != null)
{
copy.Add(head.val);
head = head.next;
}
for (int i = 0; i < copy.Count / 2; i++)
{
if (copy[i] != copy[copy.Count - i - 1])
return false;
}
return true;
}
/// <summary>
/// Readtime: 239ms Beats: 77.23%, Memory: 62mb Beats: 52.87%
/// </summary>
public bool IsPalindrome1(ListNode head)
{
if (head == null || head.next == null)
return true;
//Find the middle
ListNode s = head, f = head;
while (f.next != null && f.next.next != null)
{
s = s.next;
f = f.next.next;
}
//Reverse the second half
ListNode t = new ListNode(0, s.next), t1 = new ListNode(0, s.next);
while (t.next.next != null)
{
s.next = t.next.next;
t.next.next = t.next.next.next;
s.next.next = t1.next;
t1.next = s.next;
}
//check first half with second half
while (t1.next != null)
{
if (head.val != t1.next.val)
return false;
head = head.next;
t1.next = t1.next.next;
}
return true;
}
}
}