14_234. 回文链表
作者:互联网
题目描述:
解题思路:
- 和反转链表思路类似,把整个链表反转过来,然后比较。这里开辟了O(n)的空间,存储ListNode
- 看了题解,使用的是数组存储这些值,然后设置两个指针,头尾指针依次比较,比使用链表反转要方便
- 递归:这个题解看了半天,不是很理解,自己对第一个if条件句的理解大致就是
如果currNode.val != frontNode.val就一直返回false
- "快慢指针":先使用快慢指针定位到链表的中间点,然后反转中间点之后的链表,比对这两半的链表是否一致即可;题解中最后又反转了一次后面一半的链表,使链表复原,更完美了点。
代码:
反转整个链表
//反转整个链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if (head == null){
return true;
}
ListNode reverseList = null;
ListNode temp = head;
while (temp != null){
reverseList = new ListNode(temp.val, reverseList);
temp = temp.next;
}
ListNode p1 = reverseList;
temp = head;
while (temp != null){
if (temp.val == p1.val){
temp = temp.next;
p1 = p1.next;
}else {
return false;
}
}
return true;
}
}
利用数组存储,然后双指针头尾比较
//利用数组存储,然后双指针头尾比较
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
ArrayList<Integer> arrayList = new ArrayList<>();
while (head != null) {
arrayList.add(head.val);
head = head.next;
}
int begin = 0;
int end = arrayList.size()-1;
for (;begin <= end; begin++,end--){
if (arrayList.get(begin) != arrayList.get(end)){
return false;
}
}
return true;
}
}
递归
//递归
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
private ListNode frontNode;
public boolean isPalindrome(ListNode head) {
frontNode = head;
return recursiveCheck(head);
}
private boolean recursiveCheck (ListNode currNode) {
if (currNode != null) {
if (!recursiveCheck(currNode.next)) { //如果currNode.val != frontNode.val就一直返回false
return false;
}
if (currNode.val != frontNode.val){
return false;
}
frontNode = frontNode.next;
}
return true;
}
}
快慢指针,反转一半链表
//快慢指针,反转一半链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if (head == null || head.next == null) {
return true;
}
ListNode slow = head;
ListNode fast = head.next;
while (fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
ListNode rev = reverseList(slow.next);
ListNode p1 = head;
while (rev != null) {
if (rev.val != p1.val){
return false;
}
rev = rev.next;
p1 = p1.next;
}
return true;
}
private ListNode reverseList(ListNode head){
ListNode prev = null;
ListNode cur = head;
while (cur != null){
ListNode temp = cur.next;
cur.next = prev;
prev = cur;
cur = temp;
}
return prev;
}
}
标签:head,ListNode,14,val,int,next,链表,234 来源: https://www.cnblogs.com/forrestyu/p/15975377.html