首页 > 其他分享 >234. Palindrome Linked List

234. Palindrome Linked List

时间:2022-11-27 17:14:58浏览次数:42  
标签:head slow null List fast next Palindrome 234 prev

 

Example 1:

Input: head = [1,2,2,1]
Output: true

Example 2:

Input: head = [1,2]
Output: false
public boolean isPalindrome(ListNode head) {
ListNode slow = head, fast = head, prev, temp;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
prev = slow;
slow = slow.next;
prev.next = null;
while (slow != null) {
temp = slow.next;
slow.next = prev;
prev = slow;
slow = temp;
}
fast = head;
slow = prev;
while (slow != null) {
if (fast.val != slow.val) return false;
fast = fast.next;
slow = slow.next;
}
return true;
}

标签:head,slow,null,List,fast,next,Palindrome,234,prev
From: https://www.cnblogs.com/MarkLeeBYR/p/16930069.html

相关文章