请判断一个链表是否为回文链表。
示例 1:
输入: 1->2
输出: false
示例 2:
输入: 1->2->2->1
输出: true
进阶:
你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/palindrome-linked-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
List<Integer> list = new ArrayList<Integer>();
ListNode start = head;
while(start != null) {
list.add(start.val);
start = start.next;
}
int left = 0;//左指针
int right = list.size() - 1;//右指针
while(left < right) {
int a = list.get(left);
int b = list.get(right);
if(a == b) {
left++;
right--;
}else {
return false;
}
}
return true;
}
}