给定一个链表,请判断该链表是否为回文结构。
回文是指该字符串正序逆序完全一致。
数据范围: 链表节点数
,链表中每个节点的值满足
import java.util.*;
public class Solution {
public boolean isPail (ListNode head) {
if (head == null || head.next == null) return true;
ListNode slow = head;
ListNode fast = head.next.next;
int[] stack = new int[50000];
stack[0] = slow.val;
int offset = 0;
while (fast != null) {
slow = slow.next;
if (fast.next == null) break;
fast = fast.next.next;
stack[++offset] = slow.val;
}
slow = slow.next;
while (slow != null) {
if (slow.val != stack[offset--]) return false;
slow = slow.next;
}
return true;
}
} import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// 处理空链表(视为回文)
if (head == null) {
return true;
}
// 步骤1:将链表节点的值存入数组
List<Integer> values = new ArrayList<>();
ListNode current = head;
while (current != null) {
values.add(current.val);
current = current.next;
}
// 步骤2:使用双指针判断数组是否为回文
int left = 0;
int right = values.size() - 1;
while (left < right) {
// 若对应位置的值不相等,则不是回文
if (!values.get(left).equals(values.get(right))) {
return false;
}
left++; // 左指针右移
right--; // 右指针左移
}
// 所有对应位置的值都相等,是回文
return true;
}
}
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
List<ListNode> list=new ArrayList<ListNode>();
while(head!=null){
list.add(head);
head=head.next;
}
for(int i=0;i<list.size()/2;i++){
if(list.get(i).val!=list.get(list.size()-i-1).val){
return false;
}
}
return true;
}
} 把链表转成数组,感觉在作弊啊
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// write code here
if (head == null) return true;
// 定义一个List来存放链表的值
ArrayList<Integer> values = new ArrayList<>();
ListNode current = head;
while (current != null) {
values.add(current.val);
current = current.next;
}
// 判断列表是否为回文
int left = 0;
int right = values.size() - 1;
while (left < right) {
if (!values.get(left).equals(values.get(right))) {
return false;
}
left++;
right--;
}
return true;
}
} public class Solution {
/**
* 直接用定义:先逆序创建个链表,再依次对比。
*
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// write code here
if(head==null)return true;
ListNode revser = new ListNode(head.val);
ListNode cur = head;
while(cur.next!=null){
cur = cur.next;
ListNode temp = new ListNode(cur.val);
temp.next = revser;
revser = temp;
}
cur=head;
while(cur!=null){
if(revser.val!=cur.val)return false;
cur = cur.next;
revser = revser.next;
}
return true;
}
}
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// write code here
ArrayList<Integer> array = new ArrayList<>();
while (head != null) {
array.add(head.val);
head = head.next;
}
if (array.size() == 1) {
return true;
}
if (array.size() % 2 == 0) {
for (int i = 0; i < array.size() / 2 ; i++) {
if (Math.abs(array.get(i)) !=Math.abs(array.get(array.size() -1 - i))) {
return false;
}
}
} else {
for (int i = 0 ; i <= array.size() / 2; i++) {
if (Math.abs(array.get(i)) !=Math.abs(array.get(array.size() -1 - i))) {
return false;
}
}
}
return true;
}
} public class Solution {
public boolean isPail (ListNode head) {
// write code here
List<Integer> list = new ArrayList<Integer>();
while(head!=null){
list.add(head.val);
head=head.next;
}
int length = list.size();
for(int i=0;i<length;i++){
if((int)list.get(i) != (int)list.get(length-1-i)){
return false;
}
}
return true;
}
} public boolean isPail (ListNode head) {
return isPail2(head);
}
//破坏链表
public boolean isPail2 (ListNode head) {
if(head.next == null) return true;
ListNode new_head = new ListNode(999);
ListNode slow = head,fast = head.next;
while(true){
if(fast == null){ // 奇数个
slow = slow.next;
break;
}
if(fast.next == null){ // 偶数个
ListNode tmp = slow.next;
slow.next = new_head.next;
new_head.next = slow;
slow = tmp;
break;
}
ListNode tmp = slow.next;
slow.next = new_head.next;
new_head.next = slow;
slow = tmp;
fast = fast.next.next;
}
fast = new_head.next;
while(fast != null){
if(fast.val != slow.val ) return false;
fast = fast.next;
slow = slow.next;
}
return true;
}
//转数组
public boolean isPail1 (ListNode head) {
ArrayList<Integer> list = new ArrayList<>();
while(head != null){
list.add(head.val);
head = head.next;
}
for(int i=0,j=list.size()-1;i <= j;i++,j--){
if(list.get(i) - list.get(j) != 0){
return false;
}
}
return true;
} import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// write code here
// 使用栈获得链表逆序,并一一比较判断回文
// 算法的时间复杂度O(N),空间复杂度O(N)
// 1.对于null和单节点链表,必回文
if (head == null && head.next == null) {
return true;
}
// 2.第一次遍历,结点依次入栈
Stack<ListNode> stack = new Stack<>();
ListNode cur = head;
while (cur != null) {
stack.push(cur);
cur = cur.next;
}
// 3.第二次遍历,结点依次出栈,比较是否相同
cur = head;
while (cur != null) {
ListNode stackNode = stack.pop();
if (cur.val != stackNode.val) {
return false;
}
cur = cur.next;
}
// 4.若遍历完都没发现不同的结点,则是回文
return true;
}
} public boolean isPail (ListNode head) {
// write code here
ListNode p1=head;
Stack<ListNode> stack=new Stack<>();
while(p1!=null){
stack.push(p1);
p1=p1.next;
}
while(!stack.isEmpty()){
if(stack.pop().val!=head.val){
return false;
}
head=head.next;
}
return stack.isEmpty();
} public boolean isPail (ListNode head) {
// write code here
if (head==null) return false;
List<Integer> list = new ArrayList<>();
list.add(head.val);
while (head.next != null) {
list.add(head.next.val);
head = head.next;
}
for(int i = 0, j = list.size() - 1; i < list.size(); i++,j--) {
if(!list.get(i).equals(list.get(j))) {
return false;
}
}
return true;
} 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.next;
while(fast!=null&&fast.next!=null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode suffixPart = slow.next;
slow.next = null;
ListNode reverseSuffixPart = reverse(suffixPart);
while(head!=null && reverseSuffixPart!=null) {
if(head.val != reverseSuffixPart.val) return false;
head = head.next;
reverseSuffixPart = reverseSuffixPart.next;
}
return true;
}
public ListNode reverse(ListNode head) {
if(head == null || head.next == null) return head;
ListNode reverseListNode = null;
ListNode tempListNode = null;
while(head!=null) {
tempListNode = head.next;
head.next = reverseListNode;
reverseListNode = head;
head = tempListNode;
}
return reverseListNode;
}
}
public class Solution {
public boolean isPail (ListNode head) {
if ( head == null || head.next == null ) return true;
ListNode right = cutList(head);
while ( head != null && right != null ) {
if ( head.val != right.val ) return false;
head = head.next;
right = right.next;
}
// left, right 一样长,或者 right 多一个
return right == null || right.next == null;
}
ListNode cutList(ListNode head) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
// 找中点
ListNode fast = dummy, slow = dummy;
while ( fast != null && fast.next != null ) {
fast = fast.next.next;
slow = slow.next;
}
// 断开
ListNode right = slow.next;
slow.next = null;
// 反转right
return reverse(right);
}
ListNode reverse(ListNode head) {
ListNode pre = null, cur = head, nxt = null;
while ( cur != null ) {
nxt = cur.next;
cur.next = pre;
pre = cur;
cur = nxt;
}
return pre;
}
}