leetcode-19 Remove Nth Node From End of List
2015-03-14 12:37
447 查看
这题的关键是如何找到倒数等N个节点
双指针fast、slow
fast先走N-1步,然后fast、slow同时走,指到fast到达链表尾部
双指针fast、slow
fast先走N-1步,然后fast、slow同时走,指到fast到达链表尾部
<span style="font-family:Microsoft YaHei;font-size:14px;">/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode *removeNthFromEnd(struct ListNode *head, int n) { if(head == NULL){ return head; } struct ListNode helper; struct ListNode *pre; struct ListNode *cur; helper.next = head; pre = &helper; cur = &helper; int i; for(i = 0; i < n; i++){ cur = cur->next; } while(cur && cur ->next){ cur = cur->next; pre = pre->next; } pre->next = pre->next->next; return helper.next; }</span>
相关文章推荐
- [Leetcode] 19 - 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 19, Easy] Remove Nth Node From End of List
- leetcode[#19 链表 标尺]Remove Nth Node From End of List
- leetcode19-Remove Nth Node From End of List
- LeetCode 19.Remove Nth Node From End of List
- LeetCode41/19 First Missing Positive/Remove Nth Node From End of List ****
- LeetCode19——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 (2 solutions)
- [Leetcode 19] 19 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_19_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 --- 19. 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