博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Palindrome Linked List
阅读量:2236 次
发布时间:2019-05-09

本文共 2401 字,大约阅读时间需要 8 分钟。

解题思路:
1,单向链表就只能从前找到后,不能反向迭代。
2,回文的特点,就是要从两边迭代到中间。
3,综合上述两个特点,可以想到把链表后半部分翻转一下(题目没说不能破坏原有结构),然后从链表的开头和中间开始,比较对应node的value
1,起始条件:lenList, curTail指向链表结尾,curMid指向链表中间,将链表后半部分翻转
2,不变式:比较curHeader++ 与 curMid++ 的value
3,结束条件:出现一次比较的结果不等,结束,返回false。或者链表curMid指向结尾NULL,结束,返回true
4,临界条件:链表为NULL,链表长度为1,这两种情况,均返回true
如何将链表翻转?
1,起始条件:p指向链表头部,t一直指向链表最初状态的那个尾部节点,q指向t->next
2,不变式:temp = p, p = temp.next, t->next = temp, temp->next = q,  q = t->next
3,结束条件:p == t时,翻转结束
4,临界条件:链表为NULL或者链表长度为1,
// 编译错误
1,Line 27: ‘header’ was not declared in this scope
2,Line 48: request for member ‘val’ in ‘curHeaderStart’, which is of pointer type ‘ListNode*’ (maybe you meant to use ‘->’ ?)
// Wrong answer
1,链表为NULL或者长度为1时,都要返回true
2,后半部分 链表 reverse的算法有问题
a,链表的reverse需要三个游标,指向头的head,指向尾的t(改指针整个过程保持不变),指向t->next的q

b,每一次将head插入到 节点 t 和节点 q之间,更新head和q,t保持不变 

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    bool isPalindrome(ListNode* head) {        if (head == NULL){            return true;        }        // find length of list        int lenList = 1;        ListNode* cursor = head;        while (cursor->next != NULL){            cursor = cursor->next;            lenList ++;        }        if (lenList == 1){            return true;        }        // get pointer to tail of list        ListNode* curTail = cursor;        // get pointer to middle of list        int midIndex = lenList/2 + lenList % 2 - 1;        ListNode* curMid = head;        while (midIndex != 0){            curMid = curMid->next;            midIndex --;        }        // reverse the last half of list        ListNode * mutableTail = curTail->next; // point to tail->next always        ListNode * temp = NULL;           // point to curMid->next always        while (curMid->next != curTail){            temp = curMid->next;            curMid->next = temp->next;            curTail->next = temp;            temp->next = mutableTail;            mutableTail = curTail->next;        }        ListNode * curHeaderStart = head;        ListNode * curMidStart = curMid->next;        while (curMidStart != NULL){            if (curHeaderStart->val != curMidStart->val)                return false;            curHeaderStart = curHeaderStart->next;            curMidStart = curMidStart->next;        }        return true;    }};

转载地址:http://ecpbb.baihongyu.com/

你可能感兴趣的文章
利用栈实现DFS
查看>>
(PAT 1019) General Palindromic Number (进制转换)
查看>>
(PAT 1073) Scientific Notation (字符串模拟题)
查看>>
(PAT 1080) Graduate Admission (排序)
查看>>
Play on Words UVA - 10129 (欧拉路径)
查看>>
mininet+floodlight搭建sdn环境并创建简答topo
查看>>
【linux】nohup和&的作用
查看>>
Set、WeakSet、Map以及WeakMap结构基本知识点
查看>>
【NLP学习笔记】(一)Gensim基本使用方法
查看>>
【NLP学习笔记】(二)gensim使用之Topics and Transformations
查看>>
【深度学习】LSTM的架构及公式
查看>>
【python】re模块常用方法
查看>>
剑指offer 19.二叉树的镜像
查看>>
剑指offer 20.顺时针打印矩阵
查看>>
剑指offer 21.包含min函数的栈
查看>>
剑指offer 23.从上往下打印二叉树
查看>>
剑指offer 25.二叉树中和为某一值的路径
查看>>
剑指offer 60. 不用加减乘除做加法
查看>>
Leetcode C++《热题 Hot 100-15》437.路径总和III
查看>>
Leetcode C++《热题 Hot 100-17》461.汉明距离
查看>>