标签:倒数 back let move 题目 etc color not leetcode
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 class Solution { 10 public: 11 ListNode *removeNthFromEnd(ListNode *head, int n) { 12 if(head == NULL) 13 return NULL; 14 ListNode *pPre = NULL; 15 ListNode *p1 = head; 16 ListNode *p2 = head; 17 for(int i=0;i<n-1;i++) 18 { 19 p2 = p2->next; 20 } 21 while(p2->next != NULL) 22 { 23 pPre = p1; 24 p1 = p1->next; 25 p2 = p2->next; 26 } 27 if (pPre == NULL)//正好要删除的就是第一个结点 28 { 29 head = p1->next; 30 delete p1; 31 } 32 else 33 { 34 pPre->next = p1->next; 35 delete p1; 36 } 37 38 return head; 39 } 40 };
leetcode链表--18、remove-nth-node-from-end-of-list(从链表中删除倒数第k个结点)
标签:倒数 back let move 题目 etc color not leetcode
原文地址:http://www.cnblogs.com/qqky/p/6919866.html