LeetCode 234. Palindrome Linked List

原题链接在这里:https://leetcode.com/problems/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?

题解:

原题要求time O(n), space O(1). 所以不能用额外空间。

先找到中点, reverse中点后面的list部分,再与head开始逐个比较val. 其中reverse部分可以参见Reverse Linked List.

Time Complexity: O(n). Space O(1).

AC Java:

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 class Solution {
10     public boolean isPalindrome(ListNode head) {
11         if(head == null || head.next == null){
12             return true;
13         }
14         
15         ListNode mid = findMid(head);
16         ListNode midNext = mid.next;
17         mid.next = null;
18         
19         midNext = reverse(midNext);
20         
21         while(head != null && midNext != null){
22             if(head.val != midNext.val){
23                 return false;
24             }
25             
26             head = head.next;
27             midNext = midNext.next;
28         }
29         
30         return true;
31     }
32     
33     private ListNode findMid(ListNode head){
34         if(head == null || head.next == null){
35             return head;
36         }
37         
38         ListNode runner = head;
39         ListNode walker = head;
40         while(runner.next != null && runner.next.next != null){
41             walker = walker.next;
42             runner = runner.next.next;
43         }
44         
45         return walker;
46     }
47     
48     private ListNode reverse(ListNode head){
49         if(head == null || head.next == null){
50             return head;
51         }
52         
53         ListNode tail = head;
54         ListNode cur = tail;
55         ListNode pre;
56         ListNode temp;
57         while(tail.next != null){
58             pre = cur;
59             cur = tail.next;
60             temp = cur.next;
61             cur.next = pre;
62             tail.next = temp;
63         }
64         
65         return cur;
66     }
67 }

AC C++:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode() : val(0), next(nullptr) {}
 7  *     ListNode(int x) : val(x), next(nullptr) {}
 8  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 9  * };
10  */
11 class Solution {
12 public:
13     bool isPalindrome(ListNode* head) {
14         if(!head || !head->next){
15             return true;
16         }
17 
18         ListNode* mid = findMid(head);
19         ListNode* midNext = mid->next;
20         mid->next = NULL;
21 
22         midNext = reverse(midNext);
23         while(head && midNext){
24             if(head->val != midNext->val){
25                 return false;
26             }
27 
28             head = head->next;
29             midNext = midNext->next;
30         }
31 
32         return true;
33     }
34 
35     ListNode* findMid(ListNode* head){
36         if(!head || !head->next){
37             return head;
38         }
39 
40         ListNode* walker = head;
41         ListNode* runner = head;
42         while(runner && runner->next && runner->next->next){
43             runner = runner->next->next;
44             walker = walker->next;
45         }
46 
47         return walker;
48     }
49 
50     ListNode* reverse(ListNode* head){
51         if(!head || !head->next){
52             return head;
53         }
54 
55         ListNode* tail = head;
56         ListNode* cur = head;
57         ListNode* pre;
58         ListNode* temp;
59         while(tail->next){
60             pre = cur;
61             cur = tail->next;
62             temp = cur->next;
63             cur->next = pre;
64             tail->next = temp;
65         }
66 
67         return cur;
68     }
69 };

AC Python:

 1 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, val=0, next=None):
 4 #         self.val = val
 5 #         self.next = next
 6 class Solution:
 7     def isPalindrome(self, head: Optional[ListNode]) -> bool:
 8         if not head or not head.next:
 9             return True
10         
11         mid = self.findMid(head)
12         midNext = mid.next
13 
14         mid.next = None
15         midNext = self.reverse(midNext)
16         while head and midNext:
17             if head.val != midNext.val:
18                 return False
19             head = head.next
20             midNext = midNext.next
21         
22         return True
23 
24     def findMid(self, head: Optional[ListNode]) -> ListNode:
25         if not head or not head.next:
26             return head
27 
28         walker = runner = head
29         while runner and runner.next and runner.next.next:
30             runner = runner.next.next
31             walker = walker.next
32         
33         return walker
34 
35     def reverse(self, head: Optional[ListNode]) -> ListNode:
36         if not head or not head.next:
37             return head
38         
39         tail = cur = head
40         pre = temp = None
41         while tail.next:
42             pre = cur
43             cur = tail.next
44             temp = cur.next
45             cur.next = pre
46             tail.next = temp
47         
48         return cur

类似Maximum Twin Sum of a Linked List.

posted @ 2015-08-29 03:19  Dylan_Java_NYC  阅读(230)  评论(0编辑  收藏  举报