leetcode-java-回文链表

题目描述

请判断一个链表是否为回文链表。

示例 1:

输入: 1->2
输出: false
示例 2:

输入: 1->2->2->1
输出: true
进阶:
你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?

//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; }
 }

我的答案

不使用额外空间复杂度(适用于面试,笔试可直接用栈)
(1)快慢指针找中点 (2)右边的链表翻转(3)起点和中点一起开始比较 (4)恢复反转链表

    //找到中间结点
    public ListNode getMid(ListNode head){
        ListNode slow = head;
        ListNode fast = head;
        //找中间结点需要记住的代码,偶数找的是偏前面的那个数
        //while(fast.!=null&&fast.next!=null)则找的是偏后面那个数
        while(fast.next!=null&&fast.next.next!=null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //反转链表
    public ListNode reverse(ListNode head){
        ListNode pre = null;
        ListNode next = null;
        ListNode cur = head;
        while(cur!=null){
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }

    //判断相等
    public Boolean isEqual(ListNode n1,ListNode n2){
        while(n1!=null&&n2!=null){
            if(n1.val!=n2.val){
                return false;
            }
            n1 = n1.next;
            n2 = n2.next;
        }
        return true;
    }

    public boolean isPalindrome(ListNode head) {
        ListNode mid = getMid(head);
        //注意使用mid.next
        ListNode right = reverse(mid.next);
        ListNode n1 = head;
        ListNode n2 = right;   //注意不要改变这两个指针,下面的判断会改变指针
        Boolean flag = isEqual(n1,n2);
        ListNode node = reverse(right);
        return flag;
    }

在这里插入图片描述

官方解答

一共为两个步骤:

1.复制链表值到数组列表中。
2.使用双指针法判断是否为回文。

在编码的过程中,注意我们比较的是节点值的大小,而不是节点本身。正确的比较方式是:node_1.val == node_2.val,而 node_1 == node_2 是错误的。

class Solution {
    public boolean isPalindrome(ListNode head) {
        List<Integer> vals = new ArrayList<Integer>();

        ListNode currentNode = head;
        while(currentNode != null){
            vals.add(currentNode.val);
            currentNode = currentNode.next;
        }

        int front = 0;
        int back = vals.size()-1;
        while(front<back){
            if(!vals.get(front).equals(vals.get(back))){
                return false;
            }
            front++;
            back--;
        }
        return true;
    }
}

在这里插入图片描述

民间高手解答

fast走到末尾,slow刚好到中间。其中pre跟随slow,帮助prepre实现前半部分反转。

public boolean isPalindrome(ListNode head) {
        if(head == null || head.next == null) {
            return true;
        }
        ListNode slow = head, fast = head;
        ListNode pre = head, prepre = null;
        while(fast != null && fast.next != null) {
            pre = slow;
            slow = slow.next;
            fast = fast.next.next;
            pre.next = prepre;
            prepre = pre;
        }
        if(fast != null) {
            slow = slow.next;
        }
        while(pre != null && slow != null) {
            if(pre.val != slow.val) {
                return false;
            }
            pre = pre.next;
            slow = slow.next;
        }
        return true;
    }

在这里插入图片描述

用栈

class Solution {
    public boolean isPalindrome(ListNode head) {
        LinkedList<Integer> stack = new LinkedList<Integer>();
        ListNode tmp = head;
        while(tmp != null){
            stack.push(tmp.val);
            tmp = tmp.next;
        }  //先顺序压栈
        ListNode tmp2 = head;
        while(tmp2 != null){
            if(tmp2.val != stack.pop()){//逆序出栈比较
                return false;
            }
            tmp2 = tmp2.next;
        }
        return true;
    }
}

转换为字符串,效率低下

class Solution {
    public boolean isPalindrome(ListNode head) {
        StringBuffer buffer = new StringBuffer();
        while (head != null) {
            buffer.append(head.val);
            head = head.next;
        }
        return strIsPalindrome(buffer.toString());
    }

    public boolean strIsPalindrome(String s) {
        int left = 0, right = s.length() - 1;
        while (left < right) {
            if (s.charAt(left) != s.charAt(right)) return false;
            left++;
            right--;
        }
        return true;
    }
}

剖析链表反转

	public static Node reverseList(Node head) {
		Node pre = null;
		Node next = null;
		while (head != null) {
			next = head.next;		
			head.next = pre;		
			pre = head;			
			head = next;			
		}
		return pre;			//返回pre而不是next
}
	public static DoubleNode reverseList(DoubleNode head) {
		DoubleNode pre = null;
		DoubleNode next = null;
		while (head != null) {
			next = head.next;			
			head.next = pre;			
			head.last = next;			
			pre = head;					
			head = next;				
		}
		return pre;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值