aboutsummaryrefslogtreecommitdiffstats
path: root/876/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to '876/main.cpp')
-rw-r--r--876/main.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/876/main.cpp b/876/main.cpp
new file mode 100644
index 0000000..e9a9288
--- /dev/null
+++ b/876/main.cpp
@@ -0,0 +1,61 @@
+
+#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;
+ }
+
+ int size = heads.size();
+ std::cout << "size:" << size << "\n";
+ return heads[(size / 2) - ((size - 1) % 2) + 1];
+ }
+};
+
+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;
+}