fork download
  1. /**
  2.  * Definition for singly-linked list.
  3.  * struct ListNode {
  4.  * int val;
  5.  * ListNode *next;
  6.  * ListNode() : val(0), next(nullptr) {}
  7.  * ListNode(int x) : val(x), next(nullptr) {}
  8.  * ListNode(int x, ListNode *next) : val(x), next(next) {}
  9.  * };
  10.  */
  11.  
  12. class Solution {
  13. public:
  14. int GCD(int num1, int num2)
  15. {
  16. if (num2 == 0)
  17. return num1;
  18. return GCD(num2, num1 % num2);
  19. }
  20.  
  21. ListNode* insertGreatestCommonDivisors(ListNode* head)
  22. {
  23. ListNode* ptr = head;
  24. while(ptr->next != nullptr)
  25. {
  26. int a = ptr->val;
  27. int b = ptr->next->val;
  28. int c = GCD(a, b);
  29.  
  30. /*** 1. Correct ***/
  31. ListNode* n = new ListNode(c);
  32. n->next = ptr->next;
  33. ptr->next = n;
  34.  
  35. /*** 2. Wrong ***/
  36. ListNode n(c, ptr->next);
  37. ptr->next = &n;
  38.  
  39. ptr = ptr->next->next;
  40. }
  41. return head;
  42. }
  43. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:21:5: error: ‘ListNode’ does not name a type
     ListNode* insertGreatestCommonDivisors(ListNode* head)
     ^~~~~~~~
stdout
Standard output is empty