自己的遍历
class Solution {
public:
ListNode* middleNode(ListNode* head) {
int count = 1;
ListNode* head1=head;
for (int i = 0;; i++) {
head = head->next;
if (head == NULL) {
break;
} else {
count++;
}
}
for (int i = 2; (i < (count / 2+1)); i++) {
head1=head1->next;
}
return head1;
}
};
快指针 慢指针做法
class Solution {
public
ListNode middleNode(ListNode head) {
ListNode p = head, q = head;
while (q != null && q.next != null) {
q = q.next.next;
p = p.next;
}
return p;
}
}
好巧妙