随笔-生活工作点滴

234.回文链表

2019-07-05  本文已影响5人  Maxinxx

题目

请判断一个链表是否为回文链表。

程序核心思想

下面的三个代码分别代表了第二和第三种方法,第二种方法写了原始版和快慢指针优化版,第三种方法没有还原回去,还原的方法很简单,题目这部分不检测,我就没写。

Tips

快指针能走两步吗?fast.next != null && fast.next.next != null
用这句话来保证。

代码

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
import java.util.Stack;

class Solution {
    public boolean isPalindrome(ListNode head) {
        Stack<Integer> stack = new Stack<Integer>();
        ListNode a = head;
        int count = 0;
        
        while(a != null){
            a = a.next;
            count++;
        }
        
        a = head;
        for(int i = 0; i < count / 2; i++){
            stack.push(a.val);
            a = a.next;
        }
        
        if(count % 2 != 0){
            a = a.next;
        }
        
        while(!stack.empty()){
            if(a.val != stack.pop()){
                return false;
            }
            a = a.next;
        }
        return true;
    }
}
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
import java.util.Stack;

class Solution {
    public boolean isPalindrome(ListNode head) {
        if(head == null)    return true;
        Stack<Integer> stack = new Stack<Integer>();
        ListNode slow = head;
        ListNode fast = head;
        
        while(fast.next != null  && fast.next.next != null){
            stack.push(slow.val);
            slow = slow.next;
            fast = fast.next.next;
        }
        if(fast.next != null){
            stack.push(slow.val);
        }
        slow = slow.next;
        
        
        while(slow != null){
            if(slow.val != stack.pop()){
                return false;
            }
            slow = slow.next;
        }
        return true;
    }
}
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
import java.util.Stack;

class Solution {
    public boolean isPalindrome(ListNode head) {
        if(head == null)    return true;
        ListNode slow = head;
        ListNode fast = head;
        while(fast.next != null  && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        
        ListNode b = slow;
        ListNode c = slow;
        if(slow.next != null){
            b = slow.next;
        }else{
            return true;
        }
        if(b.next != null){
            c = b.next;
        }else{
            if(head.val == b.val)   return true;
            return false;
        }
        
        slow.next = null;
        while(c != null){
            b.next = slow;
            slow = b;
            b = c;
            c = c.next;
        }
        b.next = slow;
        
        while(b != null && head != null){
            if(b.val != head.val)   return false;
            b = b.next;
            head = head.next;
        }
        
        return true;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读