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
| #include <iostream>
using namespace std;
struct ListNode { int val{}; ListNode* next; explicit ListNode(int x = 0) : val(x), next(nullptr) {} explicit ListNode(ListNode* next) : val(0), next(next) {} ListNode(int x, ListNode* next) : val(x), next(next) {} };
ListNode* cinListNode(int n) { auto* head = new ListNode(); ListNode* p = head;
cin >> head->val;
for (int i = 1; i < n; i++) { int num; cin >> num; p->next = new ListNode(num); p = p->next; }
return head; }
inline void coutListNode(ListNode* p) { for (; p != nullptr; p = p->next) { cout << p->val << " "; }
cout << "\b\n"; }
|