分析:
1、反转链表,然后对比,空间复杂度O(n),时间复杂度O(n)
2、将链表数据存到数组里,时间复杂度空间复杂度都是O(n)
3、存到栈里
4、每次对比从前到后找到指定位置,时间复杂度O(n^2),空间复杂度O(1)——超时
1. 反转全部链表
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// write code here
if(head == null||head.next == null) return true;
ListNode index = head;
ListNode thead = new ListNode(head.val);
ListNode p = thead;
while(index.next != null){
p.next = new ListNode(index.next.val);
index = index.next;
p = p.next;
}
p = reverseList(thead);
index = head;
while(index != null && p != null){
if(index.val != p.val) return false;
index = index.next;
p = p.next;
}
return true;
}
public ListNode reverseList(ListNode head){
if(head == null||head.next == null) return head;
ListNode curr = null;
ListNode last = null;
ListNode next = head;
while(next != null){
last = curr;
curr = next;
next = next.next;
curr.next = last;
}
return curr;
}
}
反转后半部分链表
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// write code here
if(head == null||head.next == null) return true;
ListNode slow = head;
ListNode fast = head;
ListNode p;
ListNode index;
while(fast != null && fast.next != null){//快慢指针找中间位置
fast = fast.next.next;
slow = slow.next;
}
if(fast != null) slow = slow.next;//原链表长度为奇数时,慢指针往后移一位,将中间一位错开
p = reverseList(slow);
index = head;
while(index != null && p != null){
if(index.val != p.val) return false;
index = index.next;
p = p.next;
}
return true;
}
public ListNode reverseList(ListNode head){
if(head == null||head.next == null) return head;
ListNode curr = null;
ListNode last = null;
ListNode next = head;
while(next != null){
last = curr;
curr = next;
next = next.next;
curr.next = last;
}
return curr;
}
}
2. 放数组里
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// write code here
if(head == null||head.next == null)return true;
int len = 0;
ListNode index = head;
while(index != null){
index = index.next;
len++;
}
int[] tmp = new int[len];
index = head;
for(int i = 0;i < len;i++){
tmp[i] = index.val;
index = index.next;
}
for(int i = 0; i < len/2;i++){
if(tmp[i]!=tmp[len-i-1]) return false;
}
return true;
}
}
3. 全部入栈
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// write code here
ListNode index = head;
Stack<Integer> stack = new Stack<Integer>();
while(index != null){
stack.push(index.val);
index = index.next;
}
index = head;
while(stack.size() != 0){
if(stack.pop() != index.val) return false;
index = index.next;
}
return true;
}
}
4.
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// write code here
if(head == null || head.next == null) return true;
ListNode index = head;
ListNode t = head;
int len = 0;
while(index != null){
len++;
index = index.next;
}
index = head;
int cnt = 0;
for(int i = 1;i <= len/2;i++){
int key = index.val;
cnt = len + 1 - i;
t = head;
while(cnt != 1){
t = t.next;
cnt--;
}
if(key != t.val)
return false;
index = index.next;
}
return true;
}
}
提交结果:运行超时 运行时间:8001ms 占用内存:132128KB 使用语言:Java 用例通过率:84.62%