aboutsummaryrefslogtreecommitdiff
path: root/middle_linked_list.cpp
blob: ba3a2a03b89a97c3be4f5449b253b798985cba4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "leetcode.h"

class Solution {
public:
  ListNode *middleNode(ListNode *head) {
    ListNode *tmp = head;
    int len = 0;
    while (tmp) {
      len++;
      tmp = tmp->next;
    }
    int mid = len / 2;
    tmp = head;
    while (tmp && mid--)
      tmp = tmp->next;
    return tmp;
  }
};