LeetCode(234) Palindrome Linked List
1、用快慢指標找到連結串列中心位置
如果連結串列長度為偶數個,比如1,2,3,4,最後找到3
如果連結串列長度為奇數個,比如1,2,3,4,5,最好找到3
2、將後半段反轉
3、從一頭一尾狀態開始判斷是否為迴文
c++程式碼如下
/**
* 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;
if(head->next == NULL) return true;
if(head->next->next == NULL) {
if(head->val == head->next->val) return true;
else return false;
}
ListNode * pSlow, *pFast;
pSlow = head;
pFast = head;
while(pFast->next != NULL) {
pFast = pFast->next;
if(pFast->next) pFast = pFast->next;
pSlow = pSlow->next;
}
ListNode *p1 = pSlow;
ListNode *p2 = p1-> next;
ListNode *p3 = p2->next;
while(true) {
p2->next = p1;
p1 = p2;
p2 = p3;
if(p3 == NULL)
break;
else
p3 = p3->next;
}
pSlow->next = NULL;
ListNode *p4;
ListNode *p5;
p4 = head;
p5 = pFast;
while(p5 != NULL) {
if(p4->val != p5->val) return false;
p4 = p4->next;
p5 = p5->next;
}
return true;
}
};
相關推薦
LeetCode 234 Palindrome Linked List(回文鏈表)(*)(?)
給定 回文 val ren ace 一個 markdown track per 翻譯 給定一個單鏈表,確定它是否是回文的。 跟進: 你能夠在O(n)時間和O(1)空間下完畢它嗎? 原文 Given a singly linked list,
LeetCode 234. Palindrome Linked List (回文鏈表)
href http fas ava target 列表 listnode 一個 spa Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(
Leetcode:(234) Palindrome Linked List(java)
package LeetCode_LinkedList; /** * 題目: * Given a singly linked list, determine if it is a palindrome. * Example 1: * Input: 1-
[LeetCode] 234. Palindrome Linked List
題:https://leetcode.com/problems/palindrome-linked-list/description/ 題目 Given a singly linked list, determine if it is a palindrome. Example
LeetCode#234: Palindrome Linked List
Description Given a singly linked list, determine if it is a palindrome. class ListNode { int val; ListNode next; ListNode(int x
Leetcode 234. Palindrome Linked List
文章作者:Tyan 部落格:noahsnail.com | CSDN | 簡書 1. Description 2. Solution /** * Definition for singly-
【python3】leetcode 234. Palindrome Linked List (Easy)
234. Palindrome Linked List (Easy) Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false
LeetCode(234) Palindrome Linked List
1、用快慢指標找到連結串列中心位置 如果連結串列長度為偶數個,比如1,2,3,4,最後找到3 如果連結串列長度為奇數個,比如1,2,3,4,5,最好找到3 2、將後半段反轉 3、從一頭一尾狀態開始判斷是否為迴文 c++程式碼如下
【C語言】LeetCode 234. Palindrome Linked List
題目: Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(
LeetCode:234. Palindrome Linked List(判斷一個連結串列是不是迴文連結串列)
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false Example 2: Input: 1->2->2->1 Out
【LeetCode】234. Palindrome Linked List
Problem: Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? 題目:給定一個單
234. Palindrome Linked List
run eth small 比較 nodes span let tro next 旋轉鏈表函數 public ListNode reverse(ListNode head) { ListNode prev = null; while (head != null)
LC.234.Palindrome Linked List
pri div public 鏈表 head node term desc leet https://leetcode.com/problems/palindrome-linked-list/description/Given a singly linked list, d
234 Palindrome Linked List 回文鏈表
etc pro head n) sin nullptr link int {} 請檢查一個鏈表是否為回文鏈表。 進階:你能在 O(n) 的時間和 O(1) 的額外空間中做到嗎? 詳見:https://leetcode.com/problems/palindrome-link
234. Palindrome Linked List(python+cpp)
題目: Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output:
234. Palindrome Linked List - Easy
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false Example 2: Input: 1->2->2->1 Output: true
leetcode (Palindrome Linked List)
Title: Linked List Cycle 141 Difficulty:Easy 原題leetcode地址: https://leetcode.com/problems/linked-list-cy
234. Palindrome Linked List【Easy】【判斷鏈表是否回文】
put false ali lean pre lis inf pan span Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output:
leetcode 234 回文鏈表 Palindrome Linked List
clas temp head 長度 style 時間復雜度 etc src += 要求用O(n)時間,和O(1)空間,因此思路是用本身鏈表進行判斷,既然考慮回文,本方法思想是先遍歷一次求鏈表長度,然後翻轉前半部分鏈表;然後同時對前半部分鏈表和後半部分鏈表遍歷,來判斷對應
leetcode 234. 回文鏈表(Palindrome Linked List)
int false ref val pre struct while 回文 一個 目錄 題目描述: 示例 1: 示例 2: 進階: 解法: