diff options
author | terminaldweller <thabogre@gmail.com> | 2022-04-04 16:36:22 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-04-04 16:36:22 +0000 |
commit | 2fe0e87bd53f221b58ff862b1db3ce6f5bd5217c (patch) | |
tree | 2a2335d2ac1a702ecdbe0ebe4e481af50b433e4c /876/main.cpp | |
parent | added 13 (diff) | |
download | leetcode-2fe0e87bd53f221b58ff862b1db3ce6f5bd5217c.tar.gz leetcode-2fe0e87bd53f221b58ff862b1db3ce6f5bd5217c.zip |
update
Diffstat (limited to '')
-rw-r--r-- | 876/main.cpp | 61 |
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; +} |