[cc150] check palindrome of a singly linked list
Problem: Implement a function to check if a singly linked list is a palindrome.
思路:
最简单的方法是 Reverse and compare.
另外一种非常经典的办法是用 Recursive 的思路,把一个list看成这种形式:
0 ( 1 ( 2 ( 3 ) 2 ) 1 ) 0
0 ( 1 ( 2 ( 3 3 ) 2 ) 1 ) 0
CC150里面给出的Code,非常简洁,贴在下面:
length == 1 对应上面第一种情况
length == 2 对应上面第二种情况

其中用到了一个Wrapper class 'Result', 因为Java不支持返回值传两个变量。
class Result{
LinkedListNodde node;
boolean result;
}
[cc150] check palindrome of a singly linked list的更多相关文章
- Singly Linked List
Singly Linked List Singly linked list storage structure:typedef struct Node{ ElemType data; struct N ...
- LeetCode 206 Reverse a singly linked list.
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...
- Reverse a singly linked list
Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val ...
- 单链表反转(Singly Linked Lists in Java)
单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法 package dsa.linkedlist; public class Node<E> ...
- [TS] Implement a singly linked list in TypeScript
In a singly linked list each node in the list stores the contents of the node and a reference (or po ...
- [轉]Reverse a singly linked list
Reverse a singly linked list http://angelonotes.blogspot.tw/2011/08/reverse-singly-linked-list.html ...
- Data Structure Linked List: Function to check if a singly linked list is palindrome
http://www.geeksforgeeks.org/function-to-check-if-a-singly-linked-list-is-palindrome/ 这里的reverse可以re ...
- Singly linked list algorithm implemented by Java
Jeff Lee blog: http://www.cnblogs.com/Alandre/ (泥沙砖瓦浆木匠),retain the url when reproduced ! Thanks ...
随机推荐
- ActiveMQ(5.10.0) - Configuring the Simple Authentication Plug-in
The easiest way to secure the broker is through the use of authentication credentials placed directl ...
- Nginx - Rewrite Module
Initially, the purpose of this module (as the name suggests) is to perform URL rewriting. This mecha ...
- Android报错:The content of the adapter has changed...与Channel is unrecoverably broken and will be disposed的分析与解决办法
在Android中adapter错误: The content of the adapter has changed but ListView did not receive a notificati ...
- Android Device Orientation
最近在处理相机拍照的方向问题,在Android Device的Orientation问题上有了些疑问,就顺便写个Demo了解下Android Device Orientation究竟是怎么个判断. A ...
- C# DateTime格式化
DateTime. ToString() -- :: ToBinary() - ToFileTime() ToFileTimeUtc() ToLocalTime() -- :: ToLongDateS ...
- 第二篇、为UITableViewCell 高度自适应加速 缓存cell的高度
通过NSCache缓存已经算好的行高 @interface ZHCellHeightCalculator : NSObject //系统计算高度后缓存进cache -(void)setHeight:( ...
- 在swift中使用MJRefresh
cocoapod导入的,并且桥接已经完成,但是就是不提示方法,醉了,
- (转)一些国外优秀的elasticsearch使用案例
Github “Github使用Elasticsearch搜索20TB的数据,包括13亿的文件和1300亿行的代码” 这个不用介绍了吧,码农们都懂的,Github在2013年1月升级了他们的代码搜索, ...
- 【leetcode】5. Longest Palindromic Substring
题目描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- java访问webservice服务(二)
欢迎转载,出处http://www.cnblogs.com/shizhongtao/p/3433679.html 利用cxf的框架实现 import javax.xml.namespace.QName ...