leetcode: Remove Nth Node From End of List
2013-05-05 12:34
267 查看
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *removeNthFromEnd(ListNode *head, int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if (head == NULL) return NULL; ListNode *pCur; ListNode *pPreN; if (n == 1 && head->next == NULL) return NULL; int i = n; pCur = head; while (i > 0 && pCur->next != NULL) { pCur = pCur->next; i--; } if (i != 0) { if (i == 1) return head->next; else return NULL; } pPreN = head; while (pCur->next != NULL) { pCur = pCur->next; pPreN = pPreN->next; } pPreN->next = pPreN->next->next; return head; } };
相关文章推荐
- [leetcode] 19. Remove Nth Node From End of List
- LeetCode 之 Remove Nth Node From End of List
- [LeetCode] Remove Nth Node From End of List
- 18 leetcode - Remove Nth Node From End of List
- Leetcode: Remove Nth Node From End of List
- [LeetCode]Remove Nth Node From End of List
- leetcode(8)19. Remove Nth Node From End of List
- Remove Nth Node From End of List-LeetCode
- 【LeetCode】Remove Nth Node From End of List
- LeetCode Remove Nth Node From End of List
- LeetCode Remove Nth Node From End of List 删除倒数第n个元素
- LeetCode 19 Remove Nth Node From End of List
- LeetCode Solution 04: 19Remove Nth Node From End of List
- LeetCode 19 Remove Nth Node From End of List(从列表尾部删除第N个结点)(*)
- LeetCode 19. Remove Nth Node From End of List
- *LeetCode Remove Nth Node From End of List 双指针
- LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses
- leetcode19-Remove Nth Node From End of List
- leetcode19:Remove Nth Node From End of List
- LeetCode-Remove Nth Node From End of List