LeetCode-234.Palindrome Linked List
2016-05-15 22:51
411 查看
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?
使用栈和快慢指针
不适用stack,把后半部分链表反转
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution
{
public bool IsPalindrome(ListNode head)
{
if (head == null || head.next == null)
return true;
ListNode l1 = head,l2=head;
Stack<int> s = new Stack<int>();
while (l2!=null&&l2.next!=null)
{
s.Push(l1.val);
l1 = l1.next;
l2 = l2.next.next;
}
if (l2 != null)
l1 = l1.next;
ListNode last=reverse(l1);
l1 = head;
l2 = last;
while (l2!=null)
{
if (l1.val != l2.val)
{
reverse(last);
return false;
}
l1 = l1.next;
l2 = l2.next;
}
reverse(last);
return true;
}
private ListNode reverse(ListNode head)
{
ListNode pre=null,next;
while (head != null)
{
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
}
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?
使用栈和快慢指针
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int x) { val = x; } * } */ public bool IsPalindrome(ListNode head) { if (head == null || head.next == null) return true; ListNode l1 = head,l2=head; Stack<int> s = new Stack<int>(); while (l2!=null&&l2.next!=null) { s.Push(l1.val); l1 = l1.next; l2 = l2.next.next; } if (l2 != null)//奇数个node l1 = l1.next; while (l1!=null) { if (l1.val != s.Pop()) return false; l1 = l1.next; } return true; }
不适用stack,把后半部分链表反转
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution
{
public bool IsPalindrome(ListNode head)
{
if (head == null || head.next == null)
return true;
ListNode l1 = head,l2=head;
Stack<int> s = new Stack<int>();
while (l2!=null&&l2.next!=null)
{
s.Push(l1.val);
l1 = l1.next;
l2 = l2.next.next;
}
if (l2 != null)
l1 = l1.next;
ListNode last=reverse(l1);
l1 = head;
l2 = last;
while (l2!=null)
{
if (l1.val != l2.val)
{
reverse(last);
return false;
}
l1 = l1.next;
l2 = l2.next;
}
reverse(last);
return true;
}
private ListNode reverse(ListNode head)
{
ListNode pre=null,next;
while (head != null)
{
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
}
相关文章推荐
- Mysql Error Code : 1436 Thread stack overrun
- 基于java中stack与heap的区别,java中的垃圾回收机制的相关介绍
- Python算法之栈(stack)的实现
- Stack数据结构的特点后进先出的应用:大数据运算
- C 堆栈,运行时类型挷定
- jstack和线程dump分析
- c语言stack实现--数据域与指针域分开
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- 深入Java虚拟机
- 搭建CloudStack
- 新手,正在学Java Collection,瞎写点东西-一个基于链表的stack及其遍历
- 转贴一份挺好的堆和栈区别的资料
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart