关闭

【Leetcode】Palindrome Linked List

335人阅读 评论(0) 收藏 举报
分类:

题目链接:https://leetcode.com/problems/palindrome-linked-list/

题目:

Given a singly linked list, determine if it is a palindrome.

Follow up:
Could you do it in O(n) time and O(1) space?

思路:

1、用栈存储然后比较,无需改动链表结构,不过空间复杂度为O(n)

2、修改链表结构。找到中间结点,反转中间结点一边链表,然后中间结点两边链表比较。

算法1:

	public boolean isPalindrome(ListNode head) {
		Stack<Integer> stack = new Stack<Integer>();
		ListNode p = head;
		while (p != null) {
			stack.push(p.val);
			p = p.next;
		}
		p = head;
		while (p != null) {
			int tmp = stack.pop();
			if (tmp != p.val) {
				return false;
			}
			p = p.next;
		}
		return true;
	}

算法2:

	public int getListLength(ListNode head) {
		int length = 0;
		ListNode p = head;
		while (p != null) {
			length++;
			p = p.next;
		}
		return length;
	}

	public ListNode reverseList(ListNode head) {
		ListNode p = head, q, t;
		if (p == null)
			return head;
		q = p.next;
		while (q != null) {
			t = q.next;// 保存当前要处理结点后面的一个结点
			q.next = p;
			p = q; // p是新链表头结点
			q = t;
		}
		head.next = null;// 原头结点变成尾节点
		head = p;
		return head;
	}

	public boolean isPalindrome(ListNode head) {
		if (head == null || head.next == null) {
			return true;
		}
		int length = getListLength(head);
		int i = length / 2;
		ListNode p = head, l2 = null;
		while (--i > 0)
			p = p.next;
		if (length % 2 == 0) {
			l2 = p.next;
		} else {
			l2 = p.next.next;
		}
		p.next = null;

		l2 = reverseList(l2);
		while (head != null) {
			if (head.val != l2.val) {
				return false;
			} else {
				head = head.next;
				l2 = l2.next;
			}
		}
		return true;
	}



0
0
查看评论

LeetCode 234: Palindrome Linked List (回文链表)

Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?判断一个单向链表是否是回文链表,要求O(n)的时间复杂度和O(1)的空间...
  • sunao2002002
  • sunao2002002
  • 2015-07-16 22:48
  • 11249

234. Palindrome Linked List [easy] (Python)

题目链接https://leetcode.com/problems/palindrome-linked-list/题目原文 Given a singly linked list, determine if it is a palindrome. Follow up: Could ...
  • coder_orz
  • coder_orz
  • 2016-05-03 19:09
  • 1495

leetcode 234: Palindrome Linked List

leetcode 234: Palindrome Linked List python, java, c++
  • xudli
  • xudli
  • 2015-07-14 04:40
  • 8235

LeetCode234_PalindromeLinkedList (判断是否为回文链表) Java题解

题目: Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? 题解: 判断一个链表是不是回文的,这...
  • u012249528
  • u012249528
  • 2015-07-29 11:40
  • 2953

Leetcode:234. Palindrome Linked List(JAVA)

【问题描述】 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? 【思路】 1、利用栈,将lis...
  • u010183658
  • u010183658
  • 2016-03-16 21:19
  • 403

【leetcode】234. Palindrome Linked List(Python & C++)

234. Palindrome Linked List题目链接234.1 题目描述:Given a singly linked list, determine if it is a palindrome.Follow up: Could you do it in O(n) time and O(1...
  • liuxiao214
  • liuxiao214
  • 2017-09-02 20:15
  • 394

234. Palindrome Linked List(python)

Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? 题意:给定一个链表,判断其是否为回文链表 eg:1 2 3 ...
  • qq_28119401
  • qq_28119401
  • 2016-09-18 21:45
  • 363

LeetCode - 234. Palindrome Linked List

一开始思考的方式同样是使用Stack,但是这样并没有满足题目要求的利用constant space,所以这里的想法是使用fast runner/slow runner找到Linked List的中间部位,然后反转后面的部分,进而进行比较。一个很好的分析如下图: 注意这里有个小技巧,就是当f...
  • shenzhu0127
  • shenzhu0127
  • 2016-07-04 15:27
  • 145

[leetcode] 369. Plus One Linked List 解题报告

题目链接: https://leetcode.com/problems/plus-one-linked-list/ Given a non-negative number represented as a singly linked list of digits, plus one t...
  • qq508618087
  • qq508618087
  • 2016-06-28 23:52
  • 2346

leetcode 206: Reverse Linked List

Reverse Linked List leetcode
  • xudli
  • xudli
  • 2015-05-14 06:47
  • 13722
    个人资料
    • 访问:568195次
    • 积分:8667
    • 等级:
    • 排名:第2676名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论