第15题 To Check the given linked list is palindrome or not
2012-03-03 15:36
519 查看
检查链表是否是一个palindrome?
什么是palindrome?
举个例子
12344321
这就是个palindrome,开头的1和结尾的1对应,第二位的2和倒数第二位的2对应
我查到一个不错的方法,大家可以参考一下,思路很好,利用了栈FIFO的性质
大家如果有别的好的思路,欢迎留言
什么是palindrome?
举个例子
12344321
这就是个palindrome,开头的1和结尾的1对应,第二位的2和倒数第二位的2对应
我查到一个不错的方法,大家可以参考一下,思路很好,利用了栈FIFO的性质
public static boolean isPalindrome(Node head) { Node slwptr=head; Node fstptr=head; Stack<Integer> s1=new Stack<Integer>(); while(fstptr!=null && fstptr.next!=null) { s1.push(slwptr.data); slwptr=slwptr.next; fstptr=fstptr.next.next; } if(fstptr!=null) slwptr=slwptr.next; while(slwptr!=null) { if(slwptr.data!=s1.pop()) return false; slwptr = slwptr.next; } return true; }
大家如果有别的好的思路,欢迎留言
相关文章推荐
- How To Check Whether Physical Standby is in Sync with the Primary or Not?
- 2.7 Implement a function to check if a linked list is a palindrome
- Data Structure Linked List: Function to check if a singly linked list is palindrome
- Check if the given binary tree is BST or not.
- Geeks - Check whether a given graph is Bipartite or not 二分图检查
- warning:Instance variable used while 'self' is not set to the result of '[(super or self) init…]'
- eclipse中,项目有红叉之-Project configuration is not up-to-date with pom.xml. Select: Maven->Update Project... from the project context menu or use Quick Fix
- The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid
- The origin server did not find a current representation for the target resource or is not willing to
- Mysql 出现错误The server is not configured as slave; fix in config file or with CHANGE MASTER TO
- mapred.YARNRunner: Job jar is not present. Not adding any jar to the list of resources.
- Given two binary trees, write a function to check if they are equal or not.
- Ubutun 编译完sdk 出现Target id is not valid. Use 'android list targets' to get the target ids.
- Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify pr
- Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces
- The server cannot or will not process the request due to something that is perceived to be a client
- The origin server did not find a current representation for the target resource or is not willing to
- Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
- Error: timeout expired. the timeout period elapsed prior to completion of the operation or the server is not responding (Microso
- IIS 7 The Web server is configured to not list the contents of this directory