LeetCode-Palindrome Linked List

单链表回文判断及优化实现
本文介绍了一种单链表回文判断的方法,并通过优化实现了O(n)时间复杂度和O(1)空间复杂度。通过查找中点、反转链表段并比较元素值,实现高效判断。
Given a singly linked list, determine if it is a palindrome.

Follow up:
Could you do it in O(n) time and O(1) space?
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isPalindrome(ListNode head) {
        if(head == null){
            return true;
        }
        ListNode mid=findMiddle(head);
        mid.next=reverse(mid.next);
        ListNode p1=head, p2=mid.next;
        while(p2 != null && p1 != null && p1.val == p2.val){
            p1=p1.next;
            p2=p2.next;
        }
        if(p2 == null){
            return true;
        }
        else{
            return false;
        }
        
    }
    public ListNode findMiddle(ListNode head){
        if(head ==null){
            return null;
        }
        ListNode slow=head, fast=head.next;
        while(fast != null && fast.next !=null){
            slow=slow.next;
            fast=fast.next.next;
        }
        return slow;
    }
    public ListNode reverse(ListNode head){
        ListNode prev=null;
        while(head != null){
            ListNode temp=head.next;
            head.next=prev;
            prev=head;
            head=temp;
        }
        return prev;
    }
}

 二刷, 注意findmid时候fast的起始点位和如何跳出while循环:

/**
 * 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;
        }
        ListNode mid = findMid(head);
        ListNode head2 = mid.next;
        mid.next = null;
        head2 = reverse(head2);
        
        while(head != null && head2 != null){
            if(head.val != head2.val){
                return false;
            }
            head = head.next;
            head2 = head2.next;
        }
        return true;
        
    }
    
    public ListNode findMid(ListNode head){
        if(head == null){
            return null;
        }
        ListNode slow = head;
        ListNode fast = head.next;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
        
        
    }
    
    public ListNode reverse(ListNode head){
        if(head == null){
            return null;
        }
        ListNode next = head.next;
        head.next = null;
        while(next != null){
            ListNode temp = next.next;
            next.next = head;
            head = next;
            next = temp;
        }
        return head;
        
    }
}

 

转载于:https://www.cnblogs.com/incrediblechangshuo/p/5525668.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值