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

  

 分析:

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

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

3、存到栈里

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

1. 反转全部链表

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 the head
     * @return bool布尔型
     */
    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 ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 the head
     * @return bool布尔型
     */
    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 ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */
 
public class Solution {
    /**
     *
     * @param head ListNode类 the head
     * @return bool布尔型
     */
    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 ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 the head
     * @return bool布尔型
     */
    public boolean isPail (ListNode head) {
        // write code here
        ListNode index = head;
        Stack<Integer> stack = new Stack<Integer>();
        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 ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 the head
     * @return bool布尔型
     */
    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%

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值