fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // Definition for singly-linked list node
  5. class Node {
  6. public:
  7. int data;
  8. Node* next;
  9.  
  10. // Constructor
  11. Node(int val) {
  12. data = val;
  13. next = NULL;
  14. }
  15. };
  16.  
  17. // Function to find the last node in the linked list such that position % K == 0
  18. Node* modularNode(Node* head, int k) {
  19. // Edge cases
  20. if (head == NULL || k <= 0)
  21. return NULL;
  22.  
  23. Node* result = NULL;
  24. int position = 1;
  25.  
  26. // Traverse the linked list
  27. Node* current = head;
  28. while (current != NULL) {
  29. // If current position is divisible by k, update result
  30. if (position % k == 0)
  31. result = current;
  32.  
  33. // Move to next node and increment position
  34. current = current->next;
  35. position++;
  36. }
  37.  
  38. return result;
  39. }
  40.  
  41. // Utility function to create a linked list from array
  42. Node* createLinkedList(int arr[], int n) {
  43. if (n == 0) return NULL;
  44.  
  45. Node* head = new Node(arr[0]);
  46. Node* current = head;
  47.  
  48. for (int i = 1; i < n; i++) {
  49. current->next = new Node(arr[i]);
  50. current = current->next;
  51. }
  52.  
  53. return head;
  54. }
  55.  
  56. // Utility function to print a linked list
  57. void printLinkedList(Node* head) {
  58. Node* current = head;
  59. while (current != NULL) {
  60. cout << current->data;
  61. if (current->next != NULL)
  62. cout << " -> ";
  63. current = current->next;
  64. }
  65. cout << endl;
  66. }
  67.  
  68. // Test cases
  69. void runTestCase(int testNum, int arr[], int n, int k, int expectedValue) {
  70. Node* head = createLinkedList(arr, n);
  71.  
  72. cout << "Test Case " << testNum << ":" << endl;
  73. cout << "Linked List: ";
  74. printLinkedList(head);
  75. cout << "K = " << k << endl;
  76.  
  77. Node* result = modularNode(head, k);
  78.  
  79. if (result == NULL) {
  80. cout << "Result: NULL" << endl;
  81. if (expectedValue == -1)
  82. cout << "Test PASSED" << endl;
  83. else
  84. cout << "Test FAILED. Expected: " << expectedValue << ", Got: NULL" << endl;
  85. } else {
  86. cout << "Result: Node with value " << result->data << endl;
  87. if (result->data == expectedValue)
  88. cout << "Test PASSED" << endl;
  89. else
  90. cout << "Test FAILED. Expected: " << expectedValue << ", Got: " << result->data << endl;
  91. }
  92. cout << "----------------------" << endl;
  93.  
  94. // Clean up memory
  95. while (head != NULL) {
  96. Node* temp = head;
  97. head = head->next;
  98. delete temp;
  99. }
  100. }
  101.  
  102. int main() {
  103. // Test Case 1: Example from the problem
  104. // N = 7, K = 3
  105. // Linked List = 1 -> 3 -> 2 -> 4 -> 6 -> 5 -> 7
  106. int arr1[] = {1, 3, 2, 4, 6, 5, 7};
  107. runTestCase(1, arr1, 7, 3, 6);
  108.  
  109. // Test Case 2: Another example
  110. // Linked List = 3 -> 7 -> 1 -> 9 -> 8
  111. // K = 2
  112. int arr2[] = {3, 7, 1, 9, 8};
  113. runTestCase(2, arr2, 5, 2, 9);
  114.  
  115. // Test Case 3: K larger than list length
  116. int arr3[] = {5, 8, 3, 1};
  117. runTestCase(3, arr3, 4, 5, -1);
  118.  
  119. // Test Case 4: K = 1 (every node is modular)
  120. int arr4[] = {2, 4, 6, 8};
  121. runTestCase(4, arr4, 4, 1, 8);
  122.  
  123. // Test Case 5: Single element list
  124. int arr5[] = {10};
  125. runTestCase(5, arr5, 1, 1, 10);
  126.  
  127. // Test Case 6: Empty list
  128. runTestCase(6, NULL, 0, 3, -1);
  129.  
  130. // Test Case 7: Invalid K
  131. int arr7[] = {1, 2, 3};
  132. runTestCase(7, arr7, 3, 0, -1);
  133.  
  134. return 0;
  135. }
  136.  
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
Test Case 1:
Linked List: 1 -> 3 -> 2 -> 4 -> 6 -> 5 -> 7
K = 3
Result: Node with value 5
Test FAILED. Expected: 6, Got: 5
----------------------
Test Case 2:
Linked List: 3 -> 7 -> 1 -> 9 -> 8
K = 2
Result: Node with value 9
Test PASSED
----------------------
Test Case 3:
Linked List: 5 -> 8 -> 3 -> 1
K = 5
Result: NULL
Test PASSED
----------------------
Test Case 4:
Linked List: 2 -> 4 -> 6 -> 8
K = 1
Result: Node with value 8
Test PASSED
----------------------
Test Case 5:
Linked List: 10
K = 1
Result: Node with value 10
Test PASSED
----------------------
Test Case 6:
Linked List: 
K = 3
Result: NULL
Test PASSED
----------------------
Test Case 7:
Linked List: 1 -> 2 -> 3
K = 0
Result: NULL
Test PASSED
----------------------