
소스코드
/**
 * 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도 포인터로 선언해야 한다!
'개인 공부 > 코딩테스트' 카테고리의 다른 글
| [c++][백준] 1475번 방 번호 :: seoftware (0) | 2020.04.10 | 
|---|---|
| [C++][백준] 14502번 연구소 :: seoftware (0) | 2020.04.10 | 
| [c++][leetcode] Counting Elements :: seoftware (0) | 2020.04.07 | 
| [c++][leetcode] Group Anagrams :: seoftware (0) | 2020.04.07 | 
| [C++][DP][백준] 2×n 타일링 2 :: seoftware (0) | 2020.04.07 | 
 
										
									 
										
									 
										
									 
										
									
댓글