Palindrome Linked List

Palindrome Linked List

Description

Given a singly linked list, determine if it is a palindrome.

Example1:

Input: 1->2
Output: false

Example2:

Input: 1->2->2->1
Output: true

Tags: LinkedList

解读题意

判断该链表是不是回文链表

思路1

  1. 定义一个stack,将链表中所有元素压入栈中。
  2. 遍历链表,比较pop出来的元素与当前的val值是否相等,若想等则是回文。
    该方法申明了stack,使用了额外的存储空间。
class Solution { 

       public boolean isPalindrome(ListNode head) {
        LinkedList temp = new LinkedList();
        ListNode cur = head;
        while (cur != null) {
            temp.push(cur.val);
            cur = cur.next;
        }

        while (head != null) {
            if (head.val != (int) temp.pop()) {
                return false;
            }
            head = head.next;
        }

        return true;
    }

}

思路2

  1. 定义两个指针fast和slow。当fast走到链表的末尾,slow刚好处于链表的中间位置。
  2. 判断fast是否为null,若为null则表明链表长度为偶数;否则链表长度为奇数,slow = slow.next(不比较中间数)
  3. 反转链表的后半部分(slow开始)
  4. 链表的前半部分和反转后的reserve比较,若存在值不同则不是回文链表,返回false
class Solution { 

      public boolean isPalindrome(ListNode head) {

        // 快慢指针和存储中间节点
        ListNode fast = head, slow = head;

        if (head != null && head.next != null) {

            while (fast != null && fast.next != null) {

                fast = fast.next.next;
                slow = slow.next;
            }

            // 若fast不为null,则说明head长度为奇数
            if (fast != null) {
                slow = slow.next;
            }

            ListNode reserve = reserve(slow);
            // head的前半部分
            ListNode perv = head;
            while (reserve != null) {
                if (perv.val != reserve.val) {
                    return false;
                }
                perv = perv.next;
                reserve = reserve.next;
            }

        }

        return true;
    }

    private ListNode reserve(ListNode head) {
        ListNode cur = head, prev = null;
        while (cur != null) {
            ListNode temp = cur.next;
            cur.next = prev;
            prev = cur;
            cur = temp;
        }
        head = prev;
        return head;
    }
}

leetCode汇总:https://blog.csdn.net/qingtian_1993/article/details/80588941

项目源码,欢迎star:https://github.com/mcrwayfun/java-leet-code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值