当前位置:   article > 正文

单链表OJ题:LeetCode--234.回文链表

单链表OJ题:LeetCode--234.回文链表

朋友们、伙计们,我们又见面了,今天给大家带来的是LeetCode中234题:回文链表

数据结构  :数据结构专栏

作       者  :stackY、

LeetCode:LeetCode刷题训练营

LeetCode--234.回文链表:https://leetcode.cn/problems/palindrome-linked-list/ 

牛客网 - OR36 - 链表的回文结构:https://www.nowcoder.com/practice/d281619e4b3e4a60a2cc66ea32855bfa?tpId=49&tqId=29370&rp=1&ru=/activity/oj&qru=/ta/2016test/question-ranking

目录

1.题目介绍 

2.实例演示 

3.解题思路 


1.题目介绍 

 LeetCode:

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。

牛客网:

对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构。保证链表长度小于等于900。

 对于时间复杂度和空间复杂度都是有要求的,因此我们在LeetCode上面也进行要求。

2.实例演示 

 

3.解题思路 

 链表的回文可以转化为数组的回文,将链表拷贝至一个数组,然后转化成数组的回文然后使用快慢指针就可以解决,但是它的时间复杂度是O(N),空间复杂度就是O(N),就不符合题意,因此我们得换一种思路:先找链表的中间结点(可以参考LeetCode--876.链表的中间结点),然后将后半部分逆置(可以参考LeetCode--206.反转链表)然后再依次比较前半部分和逆置后的后半部分,如果不相等则返回false,若比较完两部分之后都相等则返回true,由于我们改变了原链表,因此在比较完之后需要恢复原链表,再将后半部分逆置就恢复到了原来的链表:

 

LeetCode - 代码演示(C语言实现):

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * struct ListNode *next;
  6. * };
  7. */
  8. //找中间结点
  9. struct ListNode* middleNode(struct ListNode* head) {
  10. //快慢指针
  11. struct ListNode* fast = head;
  12. struct ListNode* slow = head;
  13. //找中间结点
  14. //如果只有一个结点也直接返回
  15. while (fast && fast->next)
  16. {
  17. fast = fast->next->next;
  18. slow = slow->next;
  19. }
  20. return slow;
  21. }
  22. //反转链表
  23. struct ListNode* reverseList(struct ListNode* head) {
  24. struct ListNode* newhead = NULL;
  25. struct ListNode* cur = head;
  26. while (cur)
  27. {
  28. //保存头结点的下一个结点
  29. struct ListNode* next = cur->next;
  30. //头插
  31. cur->next = newhead;
  32. //更新新的头
  33. newhead = cur;
  34. //迭代
  35. cur = next;
  36. }
  37. return newhead;
  38. }
  39. bool isPalindrome(struct ListNode* head) {
  40. //找中间结点
  41. struct ListNode* mid = middleNode(head);
  42. //反转
  43. struct ListNode* newmid = reverseList(mid);
  44. struct ListNode* cur = head;
  45. //判断
  46. while (newmid)
  47. {
  48. if (cur->val != newmid->val)
  49. {
  50. return false;
  51. }
  52. else
  53. {
  54. cur = cur->next;
  55. newmid = newmid->next;
  56. }
  57. }
  58. //恢复原链表
  59. reverseList(newmid);
  60. return true;
  61. }

牛客网 - 代码演示(C++实现):

  1. /*
  2. struct ListNode {
  3. int val;
  4. struct ListNode *next;
  5. ListNode(int x) : val(x), next(NULL) {}
  6. };*/
  7. class PalindromeList {
  8. public:
  9. //找中间结点
  10. struct ListNode* middleNode(struct ListNode* head)
  11. {
  12. struct ListNode* fast = head;
  13. struct ListNode* slow = head;
  14. while (fast && fast->next)
  15. {
  16. fast = fast->next->next;
  17. slow = slow->next;
  18. }
  19. return slow;
  20. }
  21. //
  22. struct ListNode* reverseList(struct ListNode* head)
  23. {
  24. struct ListNode* newhead = NULL;
  25. struct ListNode* cur = head;
  26. while (cur)
  27. {
  28. struct ListNode* next = cur->next;
  29. cur->next = newhead;
  30. newhead = cur;
  31. cur = next;
  32. }
  33. return newhead;
  34. }
  35. bool chkPalindrome(ListNode* A)
  36. {
  37. // write code here
  38. ListNode* mid = middleNode(A);
  39. ListNode* newmid = reverseList(mid);
  40. while (newmid)
  41. {
  42. if (A->val == newmid->val)
  43. {
  44. A = A->next;
  45. newmid = newmid->next;
  46. }
  47. else {
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. };

今天的博客就分享到这里,喜欢的老铁留下你的三连,感谢感谢!我们下期再见!! 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/727410
推荐阅读
相关标签
  

闽ICP备14008679号