leetcode 234 Palindrome Linked List(难易度:Easy)
来源:互联网 发布:韩国网络爆红65g 编辑:程序博客网 时间:2024/05/21 14:07
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?
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode *reverseList(struct ListNode *head);bool isPalindrome(struct ListNode* head) {if (head == NULL || head->next == NULL)//如果只有一个节点,则是回文串return true;struct ListNode *slow = head;struct ListNode *fast = head;while (fast->next != NULL && fast->next->next != NULL)//fast既不是尾节点也不是倒数第二个节点{slow = slow->next;//慢指针走一步fast = fast->next->next;//快指针走两步}slow = slow->next;struct ListNode *head2 = reverseList(slow);struct ListNode *head1 = head;while (head2 != NULL) {if (head2->val != head1->val)return false;head1 = head1->next;head2 = head2->next;}return true;}struct ListNode *reverseList(struct ListNode *head) {struct ListNode *pre = NULL;struct ListNode *next = NULL;while (head != NULL) {next = head->next;head->next = pre;pre = head;head = next;}return pre;}原题地址:https://leetcode.com/problems/palindrome-linked-list/
0 0
- leetcode 234 Palindrome Linked List(难易度:Easy)
- leetcode 206 Reverse Linked List(难易度:Easy)
- leetcode 328 Odd Even Linked List(难易度:Easy)
- leetcode 237 Delete Node in a Linked List(难易度:Easy)
- [Leetcode 234, Easy] Palindrome Linked List
- 【Leetcode-Easy-234】Palindrome Linked List
- leetcode 125 Valid Palindrome(难易度:Easy)
- leetcode 009 Palindrome Number(难易度:Easy)
- Leetcode 234. Palindrome Linked List (Easy) (cpp)
- leetcode 160 Intersection of Two Linked Lists(难易度:Easy)
- [leetcode-234]Palindrome Linked List (C)
- [leetcode-234]Palindrome Linked List (C)
- LeetCode(234) Palindrome Linked List
- Palindrome Linked List leetcode 234
- 【LeetCode】234 Palindrome Linked List
- Palindrome Linked List (leetcode 234)
- leetcode 234: Palindrome Linked List
- leetcode[234]:Palindrome Linked List
- C、置换的玩笑-------(计蒜之道 测试赛)
- HDU 2034 人见人爱A-B(水~)
- 学习《算法导论》第六章 堆排序 总结二
- Android Binder 机制
- Html5 Blob
- leetcode 234 Palindrome Linked List(难易度:Easy)
- HDU 2035 人见人爱A^B(水~)
- HDOJ 2682 Tree
- C++各数据类型取值范围
- 讯飞离线语音合成(语记|语音+)
- Android中插件开发篇之----动态加载Activity(免安装运行程序)
- healthkit简介
- 如何设置hive与hbase同步
- UFFS文件系统简介