【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

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 try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

Solution 1:reverse integer

bool isPalindrome(int x)
{
    if(x<0)return false;
    if(x==reverse(x))
        return true;
    else
        return false;
}
int reverse(int x)
{
    long long result=0;
    while(x!=0)
    {
        result=result*10+x%10;
        x/=10;
        if(result>INT_MAX)return 0;
    }
    return result;
}

Solution 2 :Integer to String

#include<string>
#include<sstream>
using namespace std;
bool isPalindrome(int x) {
    stringstream ss;
    ss<<x;
    string str=ss.str();  //string s=to_string(x);
    for(int i=0,j=str.size()-1;i<j;i++,j--){
        if(str[i]!=str[j])return false;
    }
    return true;
}

 

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
#include<iostream>
#include<vector>
using namespace std;

typedef struct ListNode{
    int val;
    ListNode *next;
    ListNode(int x):val(x),next(NULL){}
}node;

node* create()
{
    node *head=new node(-1);
    node *temp=head;
    int x;
    while(cin>>x)
    {
        node *p=new node(x);
        temp->next=p;
        temp=temp->next;
    }
    head=head->next;
    temp->next=NULL;
    return head;        
}
//===============================================================
bool isPalindrome1(node *head)
{
    /*Runtime:28ms
      tranverse once, put val into vector then judge*/
    if(!head)return true;
    vector<int> vec;
    while(head)
    {
        vec.push_back(head->val);
        head=head->next;
    }
    int length=vec.size();
    for(int i=0,j=vec.size()-1;i<j;i++,j--){
        if(vec[i]!=vec[j])
            return false;
    }
    return true;
}
//===============================================================
node* getMid(node *head)
{
    ListNode *slow=head,*fast=head;
    while(fast->next && fast->next->next)
    {
        slow=slow->next;
        fast=fast->next->next;
    }
    if(fast->next)slow=slow->next;
    return slow;
}
node* reverse(node *head)
{
    if(!head || !head->next)return head;
    node *p=head,*cur=p->next;
    while(cur)
    {
        node *post=cur->next;
        cur->next=p;
        p=cur;
        cur=post;
    }    
    head->next=NULL;
    return p;
}
bool isPalindrome2(node *head)//Runtime:28ms,拆分逆转后半链表,再同时遍历两链表
{
    if(!head)return true;
    node *mid=getMid(head);
    node *remid=reverse(mid);
    while(head && remid){
        if(head->val != remid->val)return false;
        head=head->next;
        remid=remid->next;
    }
    return true;
}
int main()
{
    node *head=create();
    cout<<isPalindrome2(head);
    system("pause");
}

 

206 - Reverse Linked List

Reverse a singly linked list.

Hint:A linked list can be reversed either iteratively or recursively. Could you implement both?

Solution 1:iteration

  such as  funtion: node* reverse(node *head)  in previous title

Solution 2:recursion

class Solution {
public:
    ListNode* reverseList(ListNode* head) {     //runtime:8ms
        if(head==NULL||head->next==NULL)return head;
        
        ListNode* p = head->next;  
        ListNode* n = reverseList(p);  
          
        head->next = NULL;  
        p->next = head;  
        return n; 
    }
};

 

转载于:https://www.cnblogs.com/irun/p/4678655.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值