所谓回文链表,指的是它的val从前往后读和从后往前读是一样的,例如1->2->3->2->1和1->2->2->1就是回文链表,而1->2->3->3->1就不是回文链表,那么如何判断一个链表是否是回文链表呢?如果我们知道如何判断一个字符串是否是回文,那么借助辅助空间使用同样的思想判断即可。
例如,使用一个ArrayList来存放遍历链表得到的val,然后使用左右指针向中间逼近判断。如果在满足left <= right的前提下,出现left和right对应索引处的值不得,那么必定不是回文链表,返回false。如果判断到最后仍然满足情况,那么返回true。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null){
return true;
}
List<ListNode> list = new ArrayList<>();
ListNode cur = head;
while(cur != null){
list.add(cur);
cur =cur.next;
}
int left = 0;
int right = list.size() - 1;
while(left < right){
if(list.get(left++).val != list.get(right--).val){
return false;
}
}
return true;
}
}
或者使用栈Stack来存放遍历链表得到的val,当遍历结束后,再次重头遍历并和栈顶元素进行比较。如果某时链表节点元素和栈顶元素不同,那么返回false;如果栈最后为空也没返回false,说明该链表为回文链表,返回true。
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null){
return true;
}
Stack<ListNode> stack = new Stack<>();
ListNode cur = head;
while(cur != null){
stack.push(cur);
cur =cur.next;
}
cur = head;
while(cur != null){
if(cur.val != stack.pop().val){
return false;
}
cur = cur.next;
}
return true;
}
}
为了进一步节省空间,可以先使用快慢指针找到链表中点,然后只将后半部分链表节点入栈。如果栈不为空,则比较栈顶元素和链表中节点的val进行判断。
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null){
return true;
}
ListNode slow = head;
ListNode fast = head;
while(fast.next != null && fast.next.next != null){
slow = slow.next;
fast = fast.next.next;
}
Stack<ListNode> stack = new Stack<>();
ListNode cur = slow;
while(cur != null){
stack.push(cur);
cur =cur.next;
}
cur = head;
while(!stack.isEmpty()){
if(cur.val != stack.pop().val){
return false;
}
cur = cur.next;
}
return true;
}
}
上面的方法都使用了额外的辅助空间,如果只能原地判断,不能使用额外的空间,就需要对链表进行改造。首先,通过快慢指针找到链表中点,然后将中点后半部分的节点逆序。过程示意图如下:
最后,分别从两头遍历链表进行判断val是否相同,进而确定源链表是否是回文链表。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if (head == null || head.next == null) {
return true;
}
// 快慢指针找中点
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
// slow指向链表中点(奇数)或是上中点(偶数)
fast = slow.next; // 复用fast,指向后半部分第一个节点
slow.next = null; // 将前半部分最后一个节点的next置为null
ListNode node = null;
while (fast != null) {
node = fast.next;
fast.next = slow;
slow = fast;
fast = node;
}
node = slow; // slow指向源链表的最后一个节点
fast = head; // fast指向链表的头节点
boolean res = true;
while (slow != null && fast != null) {
if (slow.val != fast.val) {
res = false;
break;
}
slow = slow.next;
fast = fast.next;
}
// (可选)恢复源链表
// slow = node.next;
// node.next = null;
// while (slow != null) {
// fast = slow.next;
// slow.next = node;
// node = slow;
// slow = fast;
// }
return res;
}
}
本文介绍几种判断链表是否为回文的有效方法,包括使用额外空间如数组或栈,以及通过对链表进行改造实现原地判断的技术。
1723

被折叠的 条评论
为什么被折叠?



