题解 | #遍历链表# 链表
遍历链表
https://www.nowcoder.com/practice/7d348aa8b7d24e01a4f10bd023e2fb54
#include <iostream> using namespace std; int n; struct Node{ int data; Node* next; Node(int x): data(x), next(NULL){} }; int main() { while(cin >> n) { int data; Node* root = new Node(-1); while(n--) { cin >> data; Node* pre = root; Node* node = pre->next; while(node != nullptr) { if(node->data < data) { pre = node; node = node->next; } else break; } Node* tmp = new Node(data); pre->next = tmp; tmp->next = node; } while(root->next != nullptr) { cout << root->next->data << " "; root = root->next; } } }