ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

14_234. 回文链表

2022-03-07 12:31:06  阅读:143  来源: 互联网

标签:head ListNode 14 val int next 链表 234


题目描述:
image

解题思路:

  • 和反转链表思路类似,把整个链表反转过来,然后比较。这里开辟了O(n)的空间,存储ListNode
  • 看了题解,使用的是数组存储这些值,然后设置两个指针,头尾指针依次比较,比使用链表反转要方便
  • 递归:这个题解看了半天,不是很理解,自己对第一个if条件句的理解大致就是如果currNode.val != frontNode.val就一直返回false
  • "快慢指针":先使用快慢指针定位到链表的中间点,然后反转中间点之后的链表,比对这两半的链表是否一致即可;题解中最后又反转了一次后面一半的链表,使链表复原,更完美了点。

代码:

反转整个链表
//反转整个链表
/**
 * 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) {
        if (head == null){
            return true;
        }
        ListNode reverseList = null;
        ListNode temp = head;
        
        while (temp != null){
            reverseList = new ListNode(temp.val, reverseList);
            temp = temp.next;
        }
        ListNode p1 = reverseList;
        temp = head;
        while (temp != null){
            if (temp.val == p1.val){
                temp = temp.next;
                p1 = p1.next;
            }else {
                return false;
            }
        }
        return true;
    }
}
利用数组存储,然后双指针头尾比较
//利用数组存储,然后双指针头尾比较
/**
 * 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) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        while (head != null) {
            arrayList.add(head.val);
            head = head.next;
        }
        int begin = 0;
        int end = arrayList.size()-1;
        for (;begin <= end; begin++,end--){
            if (arrayList.get(begin) != arrayList.get(end)){
                return false;
            }
        }
        return true;
    }
}
递归
//递归
/**
 * 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 {
    private ListNode frontNode;
    public boolean isPalindrome(ListNode head) {
        frontNode = head;
        return recursiveCheck(head);
    }
    private boolean recursiveCheck (ListNode currNode) {
        if (currNode != null) {
            if (!recursiveCheck(currNode.next)) { //如果currNode.val != frontNode.val就一直返回false
                return false;
            } 


            if (currNode.val != frontNode.val){
                return false;
            }
            frontNode = frontNode.next;
        }
        return true;
    }
}
快慢指针,反转一半链表
//快慢指针,反转一半链表
/**
 * 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) {
        if (head == null || head.next == null) {
            return true;
        }
        ListNode slow = head;
        ListNode fast = head.next;
        while (fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }

        ListNode rev = reverseList(slow.next);
        ListNode p1 = head;
        while (rev != null) {
            if (rev.val != p1.val){
                return false;
            }
            rev = rev.next;
            p1 = p1.next;
        }
        return true;
        
        
        
    }

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

标签:head,ListNode,14,val,int,next,链表,234
来源: https://www.cnblogs.com/forrestyu/p/15975377.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有