• Leetcode-234 回文链表


    在这里插入图片描述
    在这里插入图片描述

    我的解法:使用栈,定义了len略微复杂,拿链表的后半部分和前半部分比较即可,没必要全部比较

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode() {}
     *     ListNode(int val) { this.val = val; }
     *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        public boolean isPalindrome(ListNode head) {
            ListNode head1=head;
            int len=0;
            while(head1!=null){
                len++;
                head1=head1.next;
            }
            Stack<Integer> s = new Stack<Integer>();
            int i=0;
            while(i<len/2){
                i++;
                s.push(head.val);
                head=head.next;
            }
            if(len%2==1){
                i++;
                head=head.next;
            }
            while(i<len){
                i++;
                if(head.val != s.pop()){
                    return false;
                }else{
                    head=head.next;
                }
            }
            return true;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    使用栈,和我想法类似,不定义len,所有元素全部入栈,出栈依次与头结点遍历元素相比较。

    public boolean isPalindrome(ListNode head) {
        ListNode temp = head;
        Stack<Integer> stack = new Stack();
        //把链表节点的值存放到栈中
        while (temp != null) {
            stack.push(temp.val);
            temp = temp.next;
        }
    
        //然后再出栈
        while (head != null) {
            if (head.val != stack.pop()) {
                return false;
            }
            head = head.next;
        }
        return true;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    反转后半部分链表(我应该想不到,又麻烦还要双指针…)

    public boolean isPalindrome(ListNode head) {
        ListNode fast = head, slow = head;
        //通过快慢指针找到中点
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //如果fast不为空,说明链表的长度是奇数个
        if (fast != null) {
            slow = slow.next;
        }
        //反转后半部分链表
        slow = reverse(slow);
    
        fast = head;
        while (slow != null) {
            //然后比较,判断节点值是否相等
            if (fast.val != slow.val)
                return false;
            fast = fast.next;
            slow = slow.next;
        }
        return true;
    }
    
    //反转链表
    public ListNode reverse(ListNode head) {
        ListNode prev = null;
        while (head != null) {
            ListNode next = head.next;
            head.next = prev;
            prev = head;
            head = next;
        }
        return prev;
    }
    
    作者:数据结构和算法
    链接:https://leetcode.cn/problems/palindrome-linked-list/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
  • 相关阅读:
    前端vue学习笔记——路由
    Javaweb安全——Tomcat 内存马基础
    企业级开源版本管理系统GIT分析
    数字滤波器分析---相位响应
    【学习笔记】Public NOIP Round #3 简要题解
    IDEA常用快捷键总结(Windows)
    ajax是异步还是同步?
    Springboot整合WebScoket
    Cloud Flare 添加谷歌镜像站(反向代理)
    LINUX-基础回顾
  • 原文地址:https://blog.csdn.net/qq_46574748/article/details/134276158