栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

NC96 判断一个链表是否为回文结构

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

NC96 判断一个链表是否为回文结构

  

 分析:

1、反转链表,然后对比,空间复杂度O(n),时间复杂度O(n)

2、将链表数据存到数组里,时间复杂度空间复杂度都是O(n)

3、存到栈里

4、每次对比从前到后找到指定位置,时间复杂度O(n^2),空间复杂度O(1)——超时

1. 反转全部链表

import java.util.*;



public class Solution {
    
    public boolean isPail (ListNode head) {
        // write code here
        if(head == null||head.next == null) return true;
        ListNode index = head;
        ListNode thead = new ListNode(head.val);
        ListNode p = thead;
        while(index.next != null){
            p.next = new ListNode(index.next.val);
            index = index.next;
            p = p.next;
        }
        p = reverseList(thead);
        index = head;
        while(index != null && p != null){
            if(index.val != p.val) return false;
            index = index.next;
            p = p.next;
        }
        return true;
    }
    public ListNode reverseList(ListNode head){
        if(head == null||head.next == null) return head;
        ListNode curr = null;
        ListNode last = null;
        ListNode next = head;
        while(next != null){
            last = curr;
            curr = next;
            next = next.next;
            curr.next = last;

        }
        return curr;
    }
}

 反转后半部分链表

import java.util.*;



public class Solution {
    
    public boolean isPail (ListNode head) {
        // write code here
        if(head == null||head.next == null) return true;
        ListNode slow = head;
        ListNode fast = head;
        ListNode p;
        ListNode index;
        while(fast != null && fast.next != null){//快慢指针找中间位置
            fast = fast.next.next;
            slow = slow.next;
        }
        if(fast != null) slow = slow.next;//原链表长度为奇数时,慢指针往后移一位,将中间一位错开
        
        p = reverseList(slow);
        index = head;
        while(index != null && p != null){
            if(index.val != p.val) return false;
            index = index.next;
            p = p.next;
        }
        return true;
    }
    public ListNode reverseList(ListNode head){
        if(head == null||head.next == null) return head;
        ListNode curr = null;
        ListNode last = null;
        ListNode next = head;
        while(next != null){
            last = curr;
            curr = next;
            next = next.next;
            curr.next = last;

        }
        return curr;
    }
}

2. 放数组里

import java.util.*;
 

 
public class Solution {
    
    public boolean isPail (ListNode head) {
        // write code here
        if(head == null||head.next == null)return true;
        int len = 0;
        ListNode index = head;
        while(index != null){
            index = index.next;
            len++;
        }
        int[] tmp = new int[len];
        index = head;
        for(int i = 0;i < len;i++){
            tmp[i] = index.val;
            index = index.next;
        }
        for(int i = 0; i < len/2;i++){
            if(tmp[i]!=tmp[len-i-1]) return false;
        }
        return true;
    }
}

 

 

3. 全部入栈

import java.util.*;



public class Solution {
    
    public boolean isPail (ListNode head) {
        // write code here
        ListNode index = head;
        Stack stack = new Stack();
        while(index != null){
            stack.push(index.val);
            index = index.next;
        }
        index = head;
        while(stack.size() != 0){
            if(stack.pop() != index.val) return false;
            index = index.next;
        }
        return true;
    }
}

 

4.

import java.util.*;



public class Solution {
    
    public boolean isPail (ListNode head) {
        // write code here
        if(head == null || head.next == null) return true;
        ListNode index = head;
        ListNode t = head;
        int len = 0;
        while(index != null){
            len++;
            index = index.next;
        }
        index = head;
        int cnt = 0;
        for(int i = 1;i <= len/2;i++){
            int key = index.val;
            cnt = len + 1 - i;
            t = head;
            while(cnt != 1){
                t = t.next;
                cnt--;
            }
            if(key != t.val)
                return false;
            index = index.next;
        }
        return true;
    }
}

提交结果:运行超时 运行时间:8001ms 占用内存:132128KB 使用语言:Java 用例通过率:84.62%

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/270550.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号