给定一个链表,请判断该链表是否为回文结构。
回文是指该字符串正序逆序完全一致。
数据范围: 链表节点数 ,链表中每个节点的值满足
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; } }
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 || head.next == null) { return true; } if (head.next.next == null && head.next.val == head.val) { return true; } ArrayList<Integer> list = new ArrayList<>(); ListNode tail = reverseListNode(head, list); int i = 0; while (tail != null) { if (tail.val == list.get(i)) { i++; tail = tail.next; } else { return false; } } return true; } public ListNode reverseListNode(ListNode head, ArrayList<Integer> list) { list.add(head.val);//1 2 3 4 5 if (head.next == null || head == null) { return head; } ListNode tail = reverseListNode(head.next, list); head.next.next = head; head.next = null; return tail; } }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 思路1: 反转节点后与原节点逐一对比 * @param head ListNode类 the head * @return bool布尔型 */ public boolean isPail (ListNode head) { // write code here if(head == null) { return false; } // 反转列表 ListNode rev = revNode(head); while(rev != null && rev.val == head.val) { rev = rev.next; head = head.next; } return rev == null; } /** 反转列表 */ public ListNode revNode(ListNode head) { ListNode cur = null; ListNode pre = null; while(head != null) { cur = new ListNode(head.val); cur.next = pre; pre = cur; head = head.next; } return cur; } }
public boolean isPail (ListNode head) { // write code here ListNode fast = head; ListNode slow = head; while (fast != null && fast.next != null) { fast = fast.next.next; slow = slow.next; } slow = reserve(slow); fast = head; while (slow != null) { if (slow.val != fast.val) { return false; } else { slow = slow.next; fast = fast.next; } } return true; } static ListNode reserve(ListNode head) { ListNode pre = null; ListNode cur = head; while (cur != null) { ListNode temp = cur.next; cur.next = pre; pre = cur; cur = temp; } return pre; }
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 //1、判断头节点是否为空 if (head == null) { return true; } //2、判断是否只有一个节点 if (head.next == null) { return true; } //找到链表的中间节点 ListNode fast = head; ListNode slow = head; while (fast != null && fast.next != null) { fast = fast.next.next; slow = slow.next; } ListNode cur = slow.next; ListNode pre = slow; while (cur != null) { ListNode curNext = cur.next; cur.next = pre; pre = cur; cur = curNext; } fast = head; slow = pre; while (fast != slow) { if (fast.val != slow.val) { return false; } if (fast.next == slow) { return true; } fast = fast.next; slow = slow.next; } return true; } }
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 ArrayList<Integer> list=new ArrayList<Integer>(); ListNode cur=head; while(cur!=null){//遍历整个链表到list,就是为了获得整个链表的长度 list.add(cur.val); cur=cur.next; } boolean ans=true; for(int i=0;i<(list.size()-1)/2;i++){ if(list.get(i)!=(int)list.get(list.size()-1-i)){//注意这里强转为int ans=false; break; } } return ans; } }
public boolean isPail (ListNode head) { // write code here List<Integer> list = new ArrayList<Integer>(); while (head != null) { list.add(head.val); head = head.next; } Integer[] l = new Integer[list.size()]; list.toArray(l); int start = 0; int end = list.size() - 1; while (start < end) { System.out.println(l[start] + " " + l[end]); if (!l[start++].equals(l[end--])) return false; } return true; }
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) { Stack<Integer> stack = new Stack<Integer>(); ListNode i = head; int len = 0; while (i != null) { stack.push(i.val); i = i.next; len++; } i = head; while(len-- >0){ if(stack.pop() != i.val) return false; i=i.next; } return true; } }
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 ArrayList<Integer> list = new ArrayList<Integer>(); ListNode cur = head; while (cur != null) { list.add(cur.val); cur = cur.next; } boolean res = true; for (int i = 0; i < (list.size() - 1) / 2; i++) { if (list.get(i) != (int)list.get(list.size() - i - 1)) { res = false; break; } } return res; } }