234. Palindrome Linked List*
来源:互联网 发布:网络实践教程全部答案 编辑:程序博客网 时间:2024/06/01 23:18
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?
public class Solution { public boolean isPalindrome(ListNode head) { ListNode slow=head, fast=head; while(fast!=null&&fast.next!=null){ slow = slow.next; fast = fast.next.next; } slow = reverse(slow); fast = head; while(slow!=null){ if(fast.val!=slow.val) return false; slow = slow.next; fast = fast.next; } return true; } public ListNode reverse(ListNode head){ ListNode pre =null; while(head!=null){ ListNode next = head.next; head.next = pre; pre = head; head = next; } return pre; }}总结:把后半部分反转,然后和前半部分比较。比较巧妙的地方是找到linked list 中间位置的方法。
0 0
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome (Linked List)
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 用9种办法解决 JS 闭包经典面试题之 for 循环取 i
- Vector:不定长数组基础用法
- ios 限制只能输入字母和数字
- C/C++学习资料集
- Dex Loader] Unable to execute dex: Multiple dex files define Lcom/mob/commons/MobProduct;
- 234. Palindrome Linked List*
- c++中的string常用函数用法总结
- PAT乙级1046
- Jquery实现的一个DIV层面板的折叠/展开效果
- skb_buff 详解(二)
- Asus Other Hardware\ Asus Support Device一直提醒更新问题
- 怎样在Ubuntu14.04/16.04上安装Wordpress
- 【问题】开发板挂载NFS失败
- 不定长串的字典排序