728x90
반응형
Idea
- When we need to find median value, index -> we can use this algorithms
- move two step forward agent, move onstep forward agent per scenario.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* middleNode(struct ListNode* head){
struct ListNode* first = head;
struct ListNode* second = head;
while(first && first->next)
{
second = second->next;
first = first->next->next;
}
return second;
}
728x90
반응형
'코딩 문제' 카테고리의 다른 글
[LeetCode] 1941. Check if All Characters Have Equal Number of Occurrences, C (0) | 2022.12.31 |
---|---|
[LeetCode] 383. Ransom Note, C (0) | 2022.12.30 |
[LeetCode] 1342 Number of Steps to Reduce a Number to Zero, C (0) | 2022.12.28 |
[백준] 체스판 다시 칠하기, 1018, C/C++ (0) | 2022.11.29 |
[백준] 유기농 배추, 1012, C (0) | 2022.11.28 |