注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
本帖最后由 tobias0705 于 2014-7-29 04:09 编辑
不明白这样的运行结果:
leetcode的运行结果:Line 12: expected ';' at end of member declaration。
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * ListNode *next;
- * ListNode(int x) : val(x), next(NULL) {}
- * };
- */
-
- class Solution {
- public:
- ListNode* findMid(LstNode *head);
- ListNode* reverse(ListNode *head);
- ListNode* merge(ListNode *head1, ListNode *head2);
-
- void reorderList(ListNode *head);
- };
- ListNode* Solution::findMid(ListNode *head){
- ListNode *fast = head, ListNode *slow = head;
- if (head == NULL || head->next == NULL) return head;
- while (fast->next->next != NULL && fast != NULL)
- {
- fast = fast->next->next;
- slow = slow->next;
- }
- return slow;
- }
-
- ListNode* Solution::reverse(ListNode *head){
- if (head == NULL || head->next == NULL) return head;
-
- ListNode *curNode = head, *nextNode;
- while (curNode->next != NULL)
- {
- nextNode = curNode->next;
- curNode->next = nextNode->next;
- nextNode->next = head;
- head = nextNode;
- }
- return head;
- }
-
- ListNode* Solution::merge(ListNode *head1, ListNode *head2){
- ListNode *tmp, *cur1 = head1, *cur2 = head2;
-
- while (cur2 != NULL)
- {
- tmp = cur2;
- cur2 = cur2->next;
- tmp->next = cur1->next;
- cur1->next = tmp;
- cur1 = tmp->next;
- }
- }
-
- void Solution::reorderList(ListNode *head) {
- ListNode *head2 = findMid(head), *head1 = head;
-
- head2 = reverse(head2);
- head1 = merge(head1, head2);
- head = head1;
- }
复制代码
|