LintCode 372: Delete Node in the Middle of Singly Linked List
2017-08-26 21:46
666 查看
Description:
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.
Note:
1.拿到的是当前的结点,也就是说拿不到当前结点前一个结点,所以我把当前结点用下一个结点的值覆盖掉,再删掉下一个结点。
Code:
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param node: a node in the list should be deleted
* @return: nothing
*/
void deleteNode(ListNode *node) {
// write your code here
if(!node||!node->next)
return;
ListNode * deleted=node->next;
node->val = deleted->val;
node->next = deleted ->next;
delete deleted;
return;
}
};
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.
Note:
1.拿到的是当前的结点,也就是说拿不到当前结点前一个结点,所以我把当前结点用下一个结点的值覆盖掉,再删掉下一个结点。
Code:
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param node: a node in the list should be deleted
* @return: nothing
*/
void deleteNode(ListNode *node) {
// write your code here
if(!node||!node->next)
return;
ListNode * deleted=node->next;
node->val = deleted->val;
node->next = deleted ->next;
delete deleted;
return;
}
};
相关文章推荐
- LintCode 372. Delete Node in the Middle of Singly Linked List
- 372. 在O(1)时间复杂度删除链表节点 (delete-node-in-the-middle-of-singly-linked-list)(c++)----lintcode面试题之链表
- [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点
- lintcode-easy-Delete Node in the Middle of Singly Linked List
- Delete Node in the Middle of Singly Linked List
- LinkedList---Delete Node in the Middle of Singly Linked List
- Lintcode372 Delete Node in the Middle of Singly Linked List solution 题解
- LintCode: Delete Node in the Middle of Singly Linked List
- lintcode 容易题:Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
- #372 Delete Node in the Middle of Singly Linked List
- LintCode Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
- [CrackCode] 2.3 Delete a node in the middle of a single linked list
- Q.2.3 Implement an algorithm to delete a node in the middle of a single linked list, given only acce
- 【11】Delete a node in the middle of a single linked list
- Ch2-3: remove the middle node in a singly linked list
- PIQ18: Delete middle node of a linked list
- fwnx - find the middle node in the linked list ~~~
- Ch2-2: return the nth to the last node data of a singly linked list
- 237. Delete Node in a Linked List
- leetcode之delete Node in a linked list