Remove Nth Node from End of List Problem & Solution

See the remove nth node from end of list problem on LeetCode.

C++ Solution

#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC taget("avx,avx2,fma")
#pragma GCC ivdep

static const int _=[]{ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

/**
 * struct ListNode {
 *   int val;
 *   ListNode *next;
 *   ListNode() : val(0), next(nullptr) {}
 *   ListNode(int x) : val(x), next(nullptr) {}
 *   ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
  ListNode* removeNthFromEnd(ListNode* head, int n) {
    int m = 0;
    ListNode* cursor = head;
    while (cursor != nullptr) {
      cursor = cursor->next;
      ++m;
    }

    cursor = head;
    for (int i = 0; i < m - n - 1; ++i) {
      cursor = cursor->next;
    }

    ListNode* dead;
    if (m == n) {
      dead = head;

      head = dead != nullptr ? dead->next : nullptr;
    } else {
      dead = cursor->next;

      cursor->next = dead != nullptr ? dead->next : nullptr;
    }

    delete dead;

    return head;
  }
};

Start Here

Many paths, there are. Follow yours, you must.