题干

C++实现
#include <stdio.h>
#include <list>
using namespace std;
int main() {
int s1, s2, val;
scanf("%d", &s1);
list<int> ls1, ls2;
for (int i = 0; i < s1; ++i) {
scanf("%d", &val);
ls1.push_back(val);
}
scanf("%d", &s2);
for (int i = 0; i < s2; ++i) {
scanf("%d", &val);
ls2.push_back(val);
}
list<int>::iterator it1 = ls1.begin();
list<int>::iterator it2 = ls2.begin();
list<int> ls3;
while (it1 != ls1.end() || it2 != ls2.end()) {
if (it1 == ls1.end() || (*it1 > *it2 && it2 != ls2.end()) ) {
ls3.push_back(*it2);
++it2;
} else {
ls3.push_back(*it1);
++it1;
}
}
list<int>::iterator it3;
for (it3 = ls3.begin(); it3 != ls3.end(); ++it3) {
printf("%d ", *it3);
}
printf("\n");
return 0;
}
环形链表

class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* fast = head;
ListNode* slow = head;
while(fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) {
ListNode* index1 = fast;
ListNode* index2 = head;
while (index1 != index2) {
index1 = index1->next;
index2 = index2->next;
}
return index2;
}
}
return NULL;
}
};