234.Palindrome Linked List
来源:互联网 发布:python安装后怎么用 编辑:程序博客网 时间:2024/06/10 09:28
/*
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?
*/
//解法一 51% 9ms
//思路:判断回文,首先把链表中的数放到数组中,然后进行比对即可
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
bool isPalindrome(struct ListNode* head) {
int count=0,i,j,*nums,judge = 0;
struct ListNode* p;
p = head;
while(p)
{
p = p->next;
count++;
}
p = head;
nums = (int*)malloc(sizeof(int*)*count);
for(i=0;i<count;i++)
{
nums[i] = p->val;
p = p->next;
}
for(i=0,j=count-1;i<count/2;i++,j--)
{
if(nums[i]!=nums[j])
judge = 1;
}
if(judge==1)
return false;
else
return true;
}
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?
*/
//解法一 51% 9ms
//思路:判断回文,首先把链表中的数放到数组中,然后进行比对即可
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
bool isPalindrome(struct ListNode* head) {
int count=0,i,j,*nums,judge = 0;
struct ListNode* p;
p = head;
while(p)
{
p = p->next;
count++;
}
p = head;
nums = (int*)malloc(sizeof(int*)*count);
for(i=0;i<count;i++)
{
nums[i] = p->val;
p = p->next;
}
for(i=0,j=count-1;i<count/2;i++,j--)
{
if(nums[i]!=nums[j])
judge = 1;
}
if(judge==1)
return false;
else
return true;
}
阅读全文
0 0
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome (Linked List)
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- 234. Palindrome Linked List
- JS翻转效果
- PAT-A 1003. Emergency (25)
- jquery异步请求ajax()之dataType参数讲解
- 对字符串的处理sizeof()/strlen
- http 协议
- 234.Palindrome Linked List
- 怎么进行面向对象设计(二)
- OpenCV + CentOS开发环境搭建
- 孙鑫C++教程学习记录
- Android工程师之Android面试大纲
- 2017年千禧一代奢侈品用户购买行为分析
- Friend-Graph---枚举
- C#/.Net
- android.mk中引用第三方库的方法