234 Palindrome Linked List 回文链表
请检查一个链表是否为回文链表。
进阶:
你能在 O(n) 的时间和 O(1) 的额外空间中做到吗?
详见:https://leetcode.com/problems/palindrome-linked-list/description/
Java实现:
方法一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public boolean isPalindrome(ListNode head) { //0个节点或是1个节点 if (head== null ||head!= null &&head.next== null ){ return true ; } ListNode slow=head; ListNode fast=head; Stack<Integer> stk= new Stack<Integer>(); stk.push(head.val); while (fast.next!= null &&fast.next.next!= null ){ slow=slow.next; fast=fast.next.next; stk.push(slow.val); } //链表长度为偶数 if (fast.next!= null ){ slow=slow.next; } while (slow!= null ){ int tmp=stk.pop(); if (slow.val!=tmp){ return false ; } slow=slow.next; } return true ; } } |
方法二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public boolean isPalindrome(ListNode head) { //0个节点或是1个节点 if (head== null ||head!= null &&head.next== null ){ return true ; } ListNode slow=head; ListNode fast=head; ListNode first= null ; while (fast!= null &&fast.next!= null ){ first=slow; slow=slow.next; fast=fast.next.next; } fast=first.next; first.next= null ; ListNode pre= null ; ListNode next= null ; while (fast!= null ){ next=fast.next; fast.next=pre; pre=fast; fast=next; } first=head; fast=pre; while (first!= null &&fast!= null ){ if (first.val!=fast.val){ return false ; } first=first.next; fast=fast.next; } return true ; } } |
C++实现:
方法一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | /** * 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== nullptr ||head->next== nullptr ) { return true ; } ListNode *slow=head; ListNode *fast=head; stack< int > stk; stk.push(head->val); while (fast->next&&fast->next->next) { slow=slow->next; fast=fast->next->next; stk.push(slow->val); } if (!fast->next) { stk.pop(); } while (slow->next) { slow=slow->next; int tmp=stk.top(); if (slow->val!=tmp) { return false ; } stk.pop(); } return true ; } }; |
方法二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | /** * 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||!head->next) { return true ; } ListNode *slow=head; ListNode *fast=head; ListNode *first= nullptr ; while (fast&&fast->next) { first=slow; slow=slow->next; fast=fast->next->next; } fast=first->next; first->next= nullptr ; ListNode *pre= nullptr ; ListNode *next= nullptr ; while (fast) { next=fast->next; fast->next=pre; pre=fast; fast=next; } first=head,fast=pre; while (first&&fast) { if (first->val!=fast->val) { return false ; } first=first->next; fast=fast->next; } return true ; } }; |
参考:https://www.cnblogs.com/grandyang/p/4635425.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步