public class Hello { public static void main(String[] args) { ListNode head=new ListNode(0); ListNode head1=new ListNode(1); ListNode head2=new ListNode(2); ListNode head3=new ListNode(1); ListNode head4=new ListNode(2); #region ListNode head5=new ListNode(1); ListNode head6=new ListNode(0); head5.next=head6; head4.next=head5; #endregion head3.next=head4; head2.next=head3; head1.next=head2; head.next=head1; Solution0 s =new Solution0(); boolean res=s.isPalindrome(head); System.out.println(res); // while(head!=null){ // System.out.println(head.val); // head=head.next; // } } } class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } class Solution0 { public boolean isPalindrome(ListNode head) { if (head == null || head.next == null) return true; if (head.val == head.next.val && head.next.next == null) return true; ListNode slow = head; ListNode cur = head.next; while (cur.next != null) { if (slow.val == cur.next.val) { if (cur.next.next != null) return false; cur.next = null; slow = slow.next; cur = slow.next; if (cur == null || slow.val == cur.val) return true; } else { cur = cur.next; } } return false; } } class Solution1 { public boolean isPalindrome(ListNode head) { if (head == null) return true; // detect the length int len = 0; for (ListNode p = head; p != null; p = p.next) len++; // reverse the first half list ListNode p = head, q = null, r = p.next; for (int i = 0; i < len / 2; i++) { p.next = q; q = p; p = r; r = r.next; } // detect the palindrome from the mid r = len % 2 == 0 ? p : r; while (r != null && q != null) { if (r.val != q.val) return false; r = r.next; q = q.next; } return r == null && q == null; } }
以上两个代码都不是我写的,leetcode上别人提交的java代码。
Solution0 是最快的代码,0ms。
Solution1其次。
我看S0的时候有两个问题。
Q1:不正确。
Q2:不是O(n),而是O(n2).
Q1:我本地测试。
出了一组数据,0-1-2-1-2-1-0
S0 返回false,S1 返回true。
可以认为leetcode的数据回文链表中同一个数最多只出现两次。
但是Q2我还是不懂。虽然写的是一层循环,但实际上相当于两层循环,和普通的选择排序一样。
class Solution { public: bool isPalindrome(ListNode* head) { vector<int> vec; while(head!=NULL){ vec.push_back(head->val); head=head->next; } for(int i = 0;i<vec.size()/2;i++){ if(vec[i]!=vec[vec.size()-1-i]){ return false; } } return true; } };
讨论区的。hash判断,神仙操作,看不懂。