LeetCode -- Palindrome Linked List
題目描述:
Given a singly linked list, determine if it is a palindrome.
Follow up:
Could you do it in O(n) time and O(1) space?
即判斷鏈表是否為回文。
思路:只能想到借助隊列,沒能達到空間復雜度O(1),空間復雜度O(N)
實現代碼:
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public bool IsPalindrome(ListNode head) {
if(head == null || head.next == null){
return true;
}
var list = new List();
while(head != null){
list.Add(head.val);
head = head.next;
}
for(var i =0 ;i < list.Count/2; i++){
if(list[i] != list[list.Count-i-1]){
return false;
}
}
return true;
}
}