您的位置:首页 > Web前端 > Node.js

[leetcode] 237. Delete Node in a Linked List

2016-03-08 10:47 507 查看
/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* node) {
if(!node) return;
node->val=node->next->val;
node->next=node->next->next;
}
};


Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

1. 没有告诉链表首指针,没有前指针,直接给了要删的节点指针,不需要找特定元素值的节点。

2. 把后一节点的值复制到当前节点,然后删掉后一节点。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: