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

leetCode_237. Delete Node in a Linked List

2017-10-16 00:00 190 查看
这是刷题的第一篇博客,有些话写在前面吧:

最近开始在leetCode上刷题巩固自己的数据结构知识,以及学习一些算法(提高自己能力以及为以后面试做准备),以前总觉得只要完成了任务就好,想法真是太天真,所以最近也开始关注起算法的效率问题,LeetCode是个不错的选择,还能练练阅读理解(毕竟英语渣),好,那就在博客里写写自己犯二以及有价值的题目吧!

题目描述:

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

Supposed the linked list is 
1 -> 2 -> 3 -> 4
 and you are given the third node with value 
3
,
the linked list should become 
1 -> 2 -> 4
 after calling your function.

大意是 有一个链表,给你链表其中的一个节点,删了它。开始想都没想直接node = node->next(假设node前驱节点是p,因为p->next = node)当时妄想node的前驱节点的next域会自动更新,真是zz,后面就知道了
把node后继节点的值拿过来,再改node的指针域就行了  

/**
* 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) {
node->val=node->next->val;
node->next=node->next->next;
}
};

低级错误都注意下吧,以此为戒。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: