aboutsummaryrefslogtreecommitdiffstats
path: root/876/main.cpp
blob: 20d49adb557bfb3a2ed3652de040073827c90408 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "header.hpp"

struct ListNode {
  int val;
  ListNode *next;
  ListNode() : val(0), next(nullptr) {}
  ListNode(int x) : val(x), next(nullptr) {}
  ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
  static ListNode *middleNode(ListNode *head) {
    std::vector<ListNode *> heads;
    if (nullptr == head->next)
      return head;
    while (nullptr != head->next) {
      heads.push_back(head);
      head = head->next;
    }
    heads.push_back(head);

    int size = heads.size();
    std::cout << "size:" << size << "\n";
    return heads[int(size / 2)];
  }
};

int main(int argc, char **argv) {
  ListNode *l1 = new ListNode;
  ListNode *l2 = new ListNode;
  ListNode *l3 = new ListNode;
  ListNode *l4 = new ListNode;
  ListNode *l5 = new ListNode;
  ListNode *l6 = new ListNode;
  l1->val = 1;
  l2->val = 2;
  l3->val = 3;
  l4->val = 4;
  l5->val = 5;
  l6->val = 6;
  l1->next = l2;
  l2->next = l3;
  l3->next = l4;
  l4->next = l5;
  l5->next = l6;
  l6->next = nullptr;
  ListNode *head;
  head = Solution::middleNode(l1);
  while (head->next != nullptr) {
    std::cout << head->val << ",";
    head = head->next;
  }
  std::cout << std::endl;
  delete l1;
  delete l2;
  delete l3;
  delete l4;
  delete l5;
  delete l6;
}