개인 공부/코딩테스트
[C++][leetcode] Middle of the linked list :: seoftware
seowit
2020. 4. 9. 16:25
소스코드
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* middleNode(ListNode* head) {
int sz = 0;
ListNode *cur = head;
while(cur != NULL){
sz++;
cur = cur->next;
}
sz /= 2;
cur = head;
for(int i = 0; i<sz; i++){
cur = cur->next;
}
return cur;
}
};
head가 포인터이므로 cur도 포인터로 선언해야 한다!