[LeetCode]234. Palindrome Linked List
来源:互联网 发布:社交网络可视化 编辑:程序博客网 时间:2024/06/15 08:31
Given a singly linked list, determine if it is a palindrome.
思路:快慢指针,慢指针一次跳一个,快指针一次跳两个,这样快指针跳完慢指针就是中心点
这样还是要用到n/2的栈空间,使用反转链表法可以实现1空间
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public boolean isPalindrome(ListNode head) { ListNode slow,fast; slow=head; fast=head; Stack<ListNode> stack=new Stack<ListNode>(); while(fast!=null){ stack.push(slow); slow=slow.next; fast=fast.next; if(fast==null){ stack.pop(); break; } fast=fast.next; } while(slow!=null){ if(slow.val!=stack.pop().val){ return false; } slow=slow.next; } return true; }}
0 0
- [leetcode] 234.Palindrome Linked List
- [LeetCode]234.Palindrome Linked List
- [leetcode] 234.Palindrome Linked List
- 【leetcode】234. Palindrome Linked List
- 【LeetCode】234. Palindrome Linked List
- [leetcode] 234. Palindrome Linked List
- 234. Palindrome Linked List LeetCode
- LeetCode--234. Palindrome Linked List
- LeetCode 234. Palindrome Linked List
- 【LeetCode】234. Palindrome Linked List
- leetcode 234. Palindrome Linked List
- LeetCode *** 234. Palindrome Linked List
- LeetCode 234. Palindrome Linked List
- leetcode-234. Palindrome Linked List
- LeetCode-234.Palindrome Linked List
- 【LeetCode】234. Palindrome Linked List
- [Leetcode]234. Palindrome Linked List
- 【leetcode】234. Palindrome Linked List
- manacher算法----O(n)最长回文串
- Web基础之Servlet深入
- 从jsp页面中连接数据库并进行相关操作(增删查改)
- Mysql中的where子句
- Room
- [LeetCode]234. Palindrome Linked List
- 结算方式 显示
- 抱歉了,我不做激光雷达这一块了!
- 【zzulioj 2127 tmk射气球】+ 空间点到线段距离
- bzoj 4010: [HNOI2015]菜肴制作 拓扑排序+堆
- POJ 3094 Quicksum
- css引入方式,选择器,属性,行内、块级元素--Day3
- 230. Kth Smallest Element in a BST | 二叉排序树中查找第K小的数
- HTTP通信以及Servlet详解