fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. // Definition for singly-linked list.
  5. struct ListNode {
  6. int val;
  7. ListNode *next;
  8. ListNode() : val(0), next(nullptr) {}
  9. ListNode(int x) : val(x), next(nullptr) {}
  10. ListNode(int x, ListNode *next) : val(x), next(next) {}
  11. };
  12.  
  13. class Solution {
  14. public:
  15. ListNode* removeNthFromEnd(ListNode* head, int n) {
  16. ListNode dummy(0);
  17. dummy.next = head;
  18. ListNode* first = &dummy;
  19. ListNode* second = &dummy;
  20.  
  21. // Move first n+1 steps ahead
  22. for (int i = 0; i <= n; ++i) {
  23. first = first->next;
  24. }
  25.  
  26. // Move first to the end, maintaining the gap
  27. while (first != nullptr) {
  28. first = first->next;
  29. second = second->next;
  30. }
  31.  
  32. // Skip the desired node
  33. second->next = second->next->next;
  34.  
  35. return dummy.next;
  36. }
  37. };
  38.  
  39.  
  40. ListNode* vectorToList(const std::vector<int>& values) {
  41. if (values.empty()) return nullptr;
  42. ListNode* head = new ListNode(values[0]);
  43. ListNode* current = head;
  44. for (size_t i = 1; i < values.size(); ++i) {
  45. current->next = new ListNode(values[i]);
  46. current = current->next;
  47. }
  48. return head;
  49. }
  50.  
  51. std::vector<int> listToVector(ListNode* head) {
  52. std::vector<int> result;
  53. while (head != nullptr) {
  54. result.push_back(head->val);
  55. head = head->next;
  56. }
  57. return result;
  58. }
  59.  
  60. void printList(ListNode* head) {
  61. while (head != nullptr) {
  62. std::cout << head->val << " ";
  63. head = head->next;
  64. }
  65. std::cout << std::endl;
  66. }
  67.  
  68. void testRemoveNthFromEnd() {
  69. Solution solution;
  70.  
  71. // Test case 1
  72. ListNode* head1 = vectorToList({1, 2, 3, 4, 5});
  73. ListNode* result1 = solution.removeNthFromEnd(head1, 2);
  74. std::vector<int> expected1 = {1, 2, 3, 5};
  75. std::vector<int> output1 = listToVector(result1);
  76. std::cout << "Test 1 " << (output1 == expected1 ? "passed" : "failed") << std::endl;
  77.  
  78. // Test case 2
  79. ListNode* head2 = vectorToList({1});
  80. ListNode* result2 = solution.removeNthFromEnd(head2, 1);
  81. std::vector<int> expected2 = {};
  82. std::vector<int> output2 = listToVector(result2);
  83. std::cout << "Test 2 " << (output2 == expected2 ? "passed" : "failed") << std::endl;
  84.  
  85. // Test case 3
  86. ListNode* head3 = vectorToList({1, 2});
  87. ListNode* result3 = solution.removeNthFromEnd(head3, 1);
  88. std::vector<int> expected3 = {1};
  89. std::vector<int> output3 = listToVector(result3);
  90. std::cout << "Test 3 " << (output3 == expected3 ? "passed" : "failed") << std::endl;
  91.  
  92. // Additional Test case 4
  93. ListNode* head4 = vectorToList({1, 2, 3, 4, 5});
  94. ListNode* result4 = solution.removeNthFromEnd(head4, 5);
  95. std::vector<int> expected4 = {2, 3, 4, 5};
  96. std::vector<int> output4 = listToVector(result4);
  97. std::cout << "Test 4 " << (output4 == expected4 ? "passed" : "failed") << std::endl;
  98.  
  99. // Additional Test case 5
  100. ListNode* head5 = vectorToList({1, 2, 3});
  101. ListNode* result5 = solution.removeNthFromEnd(head5, 3);
  102. std::vector<int> expected5 = {2, 3};
  103. std::vector<int> output5 = listToVector(result5);
  104. std::cout << "Test 5 " << (output5 == expected5 ? "passed" : "failed") << std::endl;
  105. }
  106.  
  107. int main() {
  108. testRemoveNthFromEnd();
  109. return 0;
  110. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
Test 1 passed
Test 2 passed
Test 3 passed
Test 4 passed
Test 5 passed