LintCode 174:Remove Nth Node From End of List
2017-08-26 21:49
477 查看
Description:
Given a linked list, remove the nth node from the end of list and return its head.
Note:
需要注意的边界情况:
当需要删除的结点是head结点时,head会发生改变。
当n超过结点个数时,表明没有需要删除的结点。
Code:
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: The first node of linked list.
* @param n: An integer.
* @return: The head of linked list.
*/
ListNode *removeNthFromEnd(ListNode *head, int n) {
// write your code here
int i;
ListNode* first = head;
ListNode* second = head;
bool flag = false;
for(i=0;i<n-1;i++){
if(first)
first=first->next;
else
break;
}
if(i<n-1||!first)
return head;
while(first->next&&first->next->next){
first=first->next;
second=second->next;
flag = true;
}
ListNode * deleted;
if(!flag){
deleted = head;
head=head->next;
}
else{
deleted = second->next;
second->next = deleted->next;
}
delete deleted;
return head;
}
};
Given a linked list, remove the nth node from the end of list and return its head.
Note:
需要注意的边界情况:
当需要删除的结点是head结点时,head会发生改变。
当n超过结点个数时,表明没有需要删除的结点。
Code:
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: The first node of linked list.
* @param n: An integer.
* @return: The head of linked list.
*/
ListNode *removeNthFromEnd(ListNode *head, int n) {
// write your code here
int i;
ListNode* first = head;
ListNode* second = head;
bool flag = false;
for(i=0;i<n-1;i++){
if(first)
first=first->next;
else
break;
}
if(i<n-1||!first)
return head;
while(first->next&&first->next->next){
first=first->next;
second=second->next;
flag = true;
}
ListNode * deleted;
if(!flag){
deleted = head;
head=head->next;
}
else{
deleted = second->next;
second->next = deleted->next;
}
delete deleted;
return head;
}
};
相关文章推荐
- 174. 删除链表中倒数第n个节点 (remove-nth-node-from-end-of-list)(c++)----lintcode面试题之链表
- [Lintcode]Remove Nth Node From End of List
- lintcode: Remove Nth Node From End of List
- [LintCode] Remove Nth Node From End of List
- #174 Remove Nth Node From End of List
- Lintcode174 Remove Nth Node From End of List solution 题解
- lintcode-easy-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 3. 移除链表的倒数第n个节点 Remove Nth Node From End of List
- Remove Nth Node From End of List(从链表表尾删除第n个节点)
- LeetCode 19 Remove Nth Node From End of List (移除距离尾节点为n的节点)
- 【Leetcode】【python】Remove Nth Node From End of List
- Remove Nth Node From End of List
- leetCode:Remove Nth Node From End of List
- Remove Nth Node From End of List,Longest Common Prefix,Palindrome Number,Roman to Integer
- 7.leetcode题目19: Remove Nth Node From End of List
- leetcode_c++:链表:Remove Nth Node From End of List(019)
- LeetCode: Remove Nth Node From End of List
- [leetcode] 19. Remove Nth Node From End of List