给定一个链表,请判断该链表是否为回文结构。
回文是指该字符串正序逆序完全一致。
数据范围: 链表节点数 ,链表中每个节点的值满足
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; } }
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; } }