fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node{
  5. int data;
  6. Node* pNext;
  7. };
  8.  
  9. void insertOrderedList(Node* &pHead, int x) {
  10. if(pHead == nullptr || pHead->data > x) {
  11. Node* tmp = new Node;
  12. tmp->data = x;
  13. tmp->pNext = pHead;
  14. pHead = tmp;
  15. return;
  16. }
  17.  
  18. Node* cur = pHead;
  19. while(cur->pNext!=nullptr) {
  20. if(cur->pNext->data > x) {
  21. Node* tmp = new Node;
  22. tmp->data = x;
  23. tmp->pNext = cur->pNext;
  24. cur->pNext = tmp;
  25. return;
  26. }
  27. cur = cur->pNext;
  28. }
  29.  
  30. Node* tmp = new Node;
  31. tmp->data = x;
  32. tmp->pNext = cur->pNext;
  33. cur->pNext = tmp;
  34. }
  35.  
  36. int main() {}
Success #stdin #stdout 0.01s 5532KB
stdin
Standard input is empty
stdout
Standard output is empty