- 一般方法:
#include <iostream> #include <vector> using namespace std; 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) {} }; void printflistnode(ListNode *head); template<class T> ListNode* constructListnode(vector<T> vec); class Solution { public: ListNode *removeNthFromEnd(ListNode *head, int n) { if (n <= 0) return head; // 计算链表长度 int len = 0; ListNode *anode = new ListNode(); anode = head; while (anode != nullptr) { len++; anode = anode->next; } // 查找并删除指定元素 if (n > len) return head; else if (n == len) { head = head->next; printflistnode(head); return head; } else { int reverse_idx = len; anode = head; while (reverse_idx != n + 1) { anode = anode->next; reverse_idx--; } anode->next = anode->next->next; } printflistnode(head); return head; } }; int main() { // 构建链表 vector<int> vec = {1,2,3,4,5}; ListNode *head=constructListnode(vec); // printf链表 printflistnode(head); cout << endl; Solution a; a.removeNthFromEnd(head, 1); // printflistnode(head); delete[] head; return 0; } void printflistnode(ListNode *head) { ListNode *next_node = head; while (next_node != nullptr) { cout << next_node->val; next_node = next_node->next; } } template<class T> ListNode* constructListnode(vector<T> vec) { ListNode* head=nullptr; if (vec.size() > 0) { head = new ListNode(vec[0]); ListNode *last_node = new ListNode(); for (int i = 1; i < vec.size(); i++) { if (i == 1) { head->next = new ListNode(vec[i]); last_node = head->next; } else { last_node->next = new ListNode(vec[i]); last_node = last_node->next; } } } return head; }
- 双指针方法:
#include <iostream> #include <vector> using namespace std; 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) {} }; int printflistnode(ListNode *head); template<class T> ListNode* constructListnode(vector<T> vec); class Solution { public: ListNode *removeNthFromEnd(ListNode *head, int n) { if(head==nullptr) return head; ListNode *fast_ptr=new ListNode(); fast_ptr=head; ListNode *slow_ptr=new ListNode(); slow_ptr=head; int idx=n; while(idx--) { fast_ptr=fast_ptr->next; if(fast_ptr==nullptr && idx!=0) return head; } ListNode *last_prt=new ListNode(); last_prt=head; while(fast_ptr!=nullptr) { fast_ptr=fast_ptr->next; last_prt=slow_ptr; slow_ptr=slow_ptr->next; } if(slow_ptr==head) head=head->next; else last_prt->next=slow_ptr->next; printflistnode(head); return head; } }; int main() { // 构建链表 vector<int> vec = {1,2,3,4,5,6}; ListNode *head=constructListnode(vec); // printf链表 printflistnode(head); cout << endl; Solution a; a.removeNthFromEnd(head, 1); // printflistnode(head); delete[] head; return 0; } int printflistnode(ListNode *head) { ListNode *next_node = head; while (next_node != nullptr) { cout << next_node->val; next_node = next_node->next; } return 0; } template<class T> ListNode* constructListnode(vector<T> vec) { ListNode* head=nullptr; if (vec.size() > 0) { head = new ListNode(vec[0]); ListNode *last_node = new ListNode(); for (int i = 1; i < vec.size(); i++) { if (i == 1) { head->next = new ListNode(vec[i]); last_node = head->next; } else { last_node->next = new ListNode(vec[i]); last_node = last_node->next; } } } return head; }
leetcode19删除链表倒数第N个节点(记录)C++
于 2023-06-19 15:27:09 首次发布