【LeetCode】234 - Palindrome Linked List

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?

Hide Tags: Linked List Two Pointers

 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4
 5 typedef struct ListNode {
 6     int val;
 7     ListNode *next;
 8 }node;
 9
10 bool isPalindrome1(ListNode* head);
11 bool isPalindrome2(ListNode* head);
12 ListNode* getMid(ListNode *head);
13 ListNode* reverse(ListNode *node);
14
15 node *create(){//建立
16     node *head,*p,*s;
17     int x,cycle=1;
18     head=new node();//head=(node*)malloc(sizeof(node));
19     p=head;
20     while(cycle){
21         cout<<"input data:";
22         cin>>x;
23         if(x!=0){
24             s=new node();
25             s->val=x;
26             p->next=s;
27             p=s;
28         }
29         else cycle=0;
30     }
31     head=head->next;
32     p->next=NULL;
33     return head;
34 }
35 int main(){
36     node *mynode=create();
37     cout<<isPalindrome2(mynode);
38 }
39
40 bool isPalindrome1(ListNode* head){  //Runtime:28ms,一次遍历,取出val,装入vector再进行判断
41     if(head==NULL||head->next==NULL)return true;
42     ListNode *node=head;
43     vector<int> vec;
44     while(node!=NULL){
45         vec.push_back(node->val);
46         node=node->next;
47     }
48     for(int i=0,j=vec.size()-1;i<j;i++,j--){
49         if(vec[i]!=vec[j])return false;
50     }
51     return true;
52 };
53
54     bool isPalindrome2(ListNode* head){  //Runtime:28ms,拆分逆转后半链表,再同时遍历两链表
55         if(head==NULL||head->next==NULL)return true;
56         ListNode *mid=getMid(head);
57         ListNode *remid=reverse(mid);
58         while(head&&remid){
59             if(head->val!=remid->val)return false;
60             head=head->next;
61             remid=remid->next;
62         }
63         return true;
64     };
65
66     ListNode* getMid(ListNode *head){
67         ListNode *slow=head,*fast=head;
68         ListNode *preslow;
69         while(fast->next){
70             fast=fast->next;
71             if(fast->next){
72                 fast=fast->next;
73                 preslow=slow;
74                 slow=slow->next;
75             }else{
76                 preslow=slow;
77                 slow=slow->next;
78             }
79         }
80         preslow->next=NULL;
81         return slow;
82     }
83     ListNode* reverse(ListNode *node){
84         if(node==NULL||node->next==NULL)return node;
85         ListNode *node1=node,*node2=node1->next,*node3;
86         while(node2){
87             node3=node2->next;
88             node2->next=node1;
89             node1=node2;
90             node2=node3;
91         }
92         node->next=NULL;
93         return node1;
94     }
时间: 2024-10-29 19:09:56

【LeetCode】234 - Palindrome Linked List的相关文章

【LeetCode】234. Palindrome Linked List (2 solutions)

Palindrome Linked List 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? 解法一: 一次遍历,装入vector,然后再一次遍历判断回文. 时间复杂度O(n),空间复杂度O(n) /** * Definition for singly-linked list. * struct ListNode

&lt;LeetCode OJ&gt; 234. Palindrome Linked List

234. Palindrome Linked List My Submissions Question Total Accepted: 33856 Total Submissions: 129644 Difficulty: Easy 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? Subscribe to see

【Leetcode】Valid Palindrome

题目链接:https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a ca

【LeetCode】009 Palindrome Number

题目:LeetCode 009 Palindrome Number 题意:判断一个整数是否为回文数,不要用额外空间 思路:我不会不用额外空间的方法,需要利用一个长度为20以内的字符串.将整数先写入一个字符串,然后判断首位字符是否相等即可. 代码如下: 1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 string s = to_string(x); 5 int len = s.size(); 6 for(int i = 0;

【Leetcode】 Odd Even Linked List

题目链接:https://leetcode.com/problems/odd-even-linked-list/ 题目: Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to

【leetcode】1278. Palindrome Partitioning III

题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindro

【LeetCode】203. Remove Linked List Elements

题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1 --> 2 --> 3 --> 4 --> 5 题解: 这道题没什么好讲的,基础操作.需要注意的是链表的第一个node,因为没有前驱节点,所以该node需

【LeetCode】9 - Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

【leetcode】92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt