leetcode Remove Nth Node from Linked List
2015-03-21 01:30
204 查看
/** * 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) { if(head == NULL){ return NULL; } ListNode* fast = head, *slow = head; for(int i = 0; i < n; ++i){ fast = fast->next; } //if we want to delete the first node of linked list if(fast == NULL){ ListNode* tmp = slow->next; delete slow; return tmp; } while(fast->next != NULL){ slow = slow->next; fast = fast->next; } ListNode* tmp = slow->next->next; ListNode* tode = slow->next; slow->next = tmp; delete tode; return head; } };
相关文章推荐
- leetcode 19. Remove Nth Node From End of List (Linked list)
- LeetCode[Linked List]: Remove Nth Node From End of List
- (*leetcode_LinkedList)Remove Nth Node From End of List
- LeetCode 019 Remove Nth Node From End of List
- LeetCode-19-Remove Nth Node From End of List(C语言实现)
- leetcode 019 —— Remove Nth Node From End of List
- leetcode Remove Nth Node From End of List java编程学习(七)
- leetcode Remove Nth Node From End of List
- Remove Nth Node From End of List ---LeetCode
- [LeetCode82]Remove Nth Node From End of List
- python实现 LeetCode19——Remove Nth Node From End of List
- [Leetcode]Remove Nth Node From End of List
- 19. Remove Nth Node From End of List LeetCode
- [leetcode] 19. Remove Nth Node From End of List python实现【easy】
- remove the nth node from the end of list leetcode
- 【leetcode】11 Remove Nth Node From End of List
- leetcode--19. Remove Nth Node From End of List
- 【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