删除指定节点Remove Nth Node From End of List
2015-01-23 17:59
162 查看
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Note:
Given n will always be valid.
Try to do this in one pass.
Hide Tags
Linked List Two Pointers
For example,
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
Hide Tags
Linked List Two Pointers
/** * 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) { ListNode *temp = head; int len=0,m; while(temp){ ++len; temp=temp->next; } if(n==len) return head->next; m=len-n; ListNode *temp1=head; while(m>1){ temp1=temp1->next; --m; } temp1->next=temp1->next->next; return head; } };
相关文章推荐
- 链表-Remove Nth Node From End of List(删除指定的节点)
- 【链表&删除倒数第K个节点】Remove Nth Node From End of List
- Remove Nth Node From End of List 删除链表倒数第N个节点
- [C++]LeetCode 19: Remove Nth Node From End of List(删除链表中倒数第n个节点)
- leetcode-19. Remove Nth Node From End of List(删除链表倒数第N个节点)
- LintCode Remove Nth Node From End of List 删除链表中倒数第n个节点
- leetCode 19.Remove Nth Node From End of List(删除倒数第n个节点) 解题思路和方法
- [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点
- [LeetCode]—Remove Nth Node From End of List 删除链表的倒数第n个节点
- LeetCode(Remove Nth Node From End of List)删除链表倒数第n个节点
- 174. 删除链表中倒数第n个节点 (remove-nth-node-from-end-of-list)(c++)----lintcode面试题之链表
- Remove Nth Node From End of List(从链表表尾删除第n个节点)
- #19 Remove Nth Node From End of List(删除链表倒数第N个节点)
- Remove Nth Node From End of List 从链表中删除倒数第N的节点
- 【LeetCode】Remove Nth Node From End of List 删除链表中倒数第n个节点- Easy +
- leetcode_[python/C++]_19. Remove Nth Node From End of List(删除链表末第n个节点)
- [LeetCode] 19. Remove Nth Node From End of List 删除链表的倒数第N个节点 @python
- 【LeetCode题解】19_删除链表的倒数第N个节点(Remove-Nth-Node-From-End-of-List)
- [LeetCode-19] Remove Nth Node From End of List(删除链表中倒数第N个节点)
- leetcode:Remove Nth Node From End of List(删除链表倒数第n个节点)【面试算法题】