标签:new fas init ++ nod 倒数 rem bsp fast
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 10 //1、删除节点,就是找到该节点的前一个节点就可以 11 //2、然后slow->next = slow->next->next; 12 class Solution 13 { 14 public: 15 ListNode* removeNthFromEnd(ListNode* head, int n) 16 { 17 ListNode* dummy = new ListNode(-1); 18 dummy->next = head; 19 20 ListNode* fast = dummy; 21 ListNode* slow = dummy; 22 for(int i = 0;i <= n;i ++) fast = fast->next; 23 while(fast) 24 { 25 fast = fast->next; 26 slow = slow->next; 27 } 28 if(slow->next == NULL) slow = NULL; 29 else slow->next = slow->next->next; 30 return dummy->next; 31 } 32 };
标签:new fas init ++ nod 倒数 rem bsp fast
原文地址:https://www.cnblogs.com/yuhong1103/p/12499194.html