[LintCode] 在O(1)时间复杂度删除链表节点

简介: 1 /** 2 * Definition of ListNode 3 * class ListNode { 4 * public: 5 * int val; 6 * ListNode *next; 7 * ListNode(int v...
 1 /**
 2  * Definition of ListNode
 3  * class ListNode {
 4  * public:
 5  *     int val;
 6  *     ListNode *next;
 7  *     ListNode(int val) {
 8  *         this->val = val;
 9  *         this->next = NULL;
10  *     }
11  * }
12  */
13 class Solution {
14 public:
15     /**
16      * @param node: a node in the list should be deleted
17      * @return: nothing
18      */
19     void deleteNode(ListNode *node) {
20         // write your code here
21         ListNode* next = node -> next;
22         node -> val = next -> val;
23         node -> next = next -> next;
24         next -> next = NULL;
25         delete next;
26     }
27 };

 

目录
相关文章
|
18天前
|
存储 Python
链表中插入节点
链表中插入节点
|
3天前
24. 两两交换链表中的节点
24. 两两交换链表中的节点
|
6天前
|
存储
删除链表的节点
删除链表的节点
5 0
|
8天前
|
存储 SQL 算法
|
8天前
|
SQL 算法 数据挖掘
力扣题目 19:删除链表的倒数第N个节点 【python】
力扣题目 19:删除链表的倒数第N个节点 【python】
|
18天前
|
存储 Python
删除链表节点详解
删除链表节点详解
|
18天前
|
存储 Python
链表中删除节点
链表中删除节点
|
7天前
|
存储 SQL 算法
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
|
4天前
|
算法
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表
|
7天前
|
存储 SQL 算法
LeetCode 题目 86:分隔链表
LeetCode 题目 86:分隔链表