lintcode--回文链表
2017-07-17 09:33
190 查看
设计一种方式检查一个链表是否为回文链表。
您在真实的面试中是否遇到过这个题?
Yes
样例
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
思路:
回文串的特征就是正反看结果都一样
如:aba、abcsdffdscba
所有翻转链表后比较
但是翻转链表改变了原始链表之间的指针
需要先复制原始链表
复制链表和选择链表进行比较
*/
public class Solution {
/**
* @param head a ListNode
* @return a boolean
*/
public boolean isPalindrome(ListNode head) {
// Write your code here
if(head == null || head.next == null)
return true;
ListNode p = head;
ListNode curHead = copy(head);
ListNode revHead = reverse(p);
boolean flag= compare(curHead,revHead);
return flag;
}
public boolean compare(ListNode l1,ListNode l2){
while(l1!=null && l2!=null){
if(l1.val!=l2.val)
return false;
l1 = l1.next;
l2 = l2.next;
}
return true;
}
public ListNode copy(ListNode head){
ListNode curHead = new ListNode(0);
ListNode cur = curHead;
ListNode p = head;
while(p!=null){
cur.next = new ListNode(p.val);
p = p.next;
cur = cur.next;
}
return curHead.next;
}
public ListNode reverse(ListNode head){
ListNode revHead = null;
ListNode cur = null;
ListNode p = head;
while(p!=null){
//反转
ListNode pNext = p.next;
if(pNext==null){
revHead = p;
}
p.next = cur;
4000
cur = p;
p = pNext;
}
return revHead;
}
}
您在真实的面试中是否遇到过这个题?
Yes
样例
1->2->1就是一个回文链表。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
思路:
回文串的特征就是正反看结果都一样
如:aba、abcsdffdscba
所有翻转链表后比较
但是翻转链表改变了原始链表之间的指针
需要先复制原始链表
复制链表和选择链表进行比较
*/
public class Solution {
/**
* @param head a ListNode
* @return a boolean
*/
public boolean isPalindrome(ListNode head) {
// Write your code here
if(head == null || head.next == null)
return true;
ListNode p = head;
ListNode curHead = copy(head);
ListNode revHead = reverse(p);
boolean flag= compare(curHead,revHead);
return flag;
}
public boolean compare(ListNode l1,ListNode l2){
while(l1!=null && l2!=null){
if(l1.val!=l2.val)
return false;
l1 = l1.next;
l2 = l2.next;
}
return true;
}
public ListNode copy(ListNode head){
ListNode curHead = new ListNode(0);
ListNode cur = curHead;
ListNode p = head;
while(p!=null){
cur.next = new ListNode(p.val);
p = p.next;
cur = cur.next;
}
return curHead.next;
}
public ListNode reverse(ListNode head){
ListNode revHead = null;
ListNode cur = null;
ListNode p = head;
while(p!=null){
//反转
ListNode pNext = p.next;
if(pNext==null){
revHead = p;
}
p.next = cur;
4000
cur = p;
p = pNext;
}
return revHead;
}
}
相关文章推荐
- LintCode:回文链表
- LintCode-剑指Offer-(105)复制带随机指针的链表
- LintCode:两两交换链表中的节点
- lintcode翻转链表
- LintCode:删除链表中的元素452
- lintcode-96-链表划分
- LintCode之35 翻转链表 标签: 链表
- LintCode-372.在O(1)时间复杂度删除链表节点
- lintcode 容易题:Reverse Linked List 翻转链表
- lintcode——链表倒数第n个节点
- LintCode-剑指Offer-(372)在O(1)时间复杂度删除链表节点
- lintcode最长回文子串(Manacher算法)
- LintCode : 删除链表中倒数第n个节点
- lintcode-有效回文字符串
- lintcode——将二叉树拆成链表
- Lintcode: 复制带随机指针的链表
- LintCode:翻转链表 II
- Lintcode:翻转链表
- LintCode—合并两个排序链表(165)
- LintCode Remove Duplicates from Sorted List 删除链表中的重复元素