234. Palindrome Linked List
MA:
注意:Integer比较要用Integer.equals
/**
* Definition for singly-linked list
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if(head==null){
return true;
}
ArrayList<Integer> temp=new ArrayList<Integer>();
while(head.next!=null){
temp.add(new Integer(head.val));
head=head.next;
}
temp.add(new Integer(head.val));
for(int i=0;i<temp.size()-1-i;i++){
if(temp.get(i).equals(temp.get(temp.size()-1-i))){
continue;
}else{
return false;
}
}
return true;
}
}
OA:
class Solution { 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; } }
AOA:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public boolean isPalindrome(ListNode head) { ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } if (fast != null) { slow = slow.next; } slow = reverse(slow); fast = head; while (slow != null) { if (fast.val != slow.val) { return false; } slow = slow.next; fast = fast.next; } return true; } private ListNode reverse(ListNode head) { ListNode prev = null; while (head !=null) { ListNode next = head.next; head.next = prev; prev = head; head = next; } return prev; } }
注意:Integer比较要用Integer.equals
/**
* Definition for singly-linked list
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
if(head==null){
return true;
}
ArrayList<Integer> temp=new ArrayList<Integer>();
while(head.next!=null){
temp.add(new Integer(head.val));
head=head.next;
}
temp.add(new Integer(head.val));
for(int i=0;i<temp.size()-1-i;i++){
if(temp.get(i).equals(temp.get(temp.size()-1-i))){
continue;
}else{
return false;
}
}
return true;
}
}
OA:
class Solution { 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; } }
AOA:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public boolean isPalindrome(ListNode head) { ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } if (fast != null) { slow = slow.next; } slow = reverse(slow); fast = head; while (slow != null) { if (fast.val != slow.val) { return false; } slow = slow.next; fast = fast.next; } return true; } private ListNode reverse(ListNode head) { ListNode prev = null; while (head !=null) { ListNode next = head.next; head.next = prev; prev = head; head = next; } return prev; } }
评论
发表评论