-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path234.IsPalindrome.cs
39 lines (37 loc) · 966 Bytes
/
234.IsPalindrome.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
// 234. Palindrome Linked List
// Given the head of a singly linked list, return true if it is a palindrome or false otherwise.
// Example 1:
// Input: head = [1,2,2,1]
// Output: true
// Example 2:
// Input: head = [1,2]
// Output: false
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public bool IsPalindrome(ListNode head) {
Stack<int> stack = new Stack<int>();
ListNode curr = head;
while(curr != null){
stack.Push(curr.val);
curr = curr.next;
}
curr = head;
int mid = stack.Count/2;
while(mid < stack.Count){
if(stack.Pop() != curr.val)
return false;
curr = curr.next;
}
return true;
}
}