forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
48 lines (45 loc) · 1.02 KB
/
Solution.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
public class Solution {
public bool IsPalindrome(ListNode head) {
if (head == null) return true;
var count = Count(head);
var temp = head;
var c = 1;
while (c < count / 2)
{
++c;
temp = temp.next;
}
var head2 = Reverse(temp.next);
temp.next = null;
while (head != null && head2 != null)
{
if (head.val != head2.val) return false;
head = head.next;
head2 = head2.next;
}
return true;
}
private int Count(ListNode head)
{
var count = 0;
while (head != null)
{
++count;
head = head.next;
}
return count;
}
private ListNode Reverse(ListNode head)
{
var temp = head;
head = null;
while (temp != null)
{
var temp2 = temp.next;
temp.next = head;
head = temp;
temp = temp2;
}
return head;
}
}