博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【LeetCode 237】Delete Node in a Linked List
阅读量:4601 次
发布时间:2019-06-09

本文共 886 字,大约阅读时间需要 2 分钟。

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.

题意:

  给定一个单向链表中的任意结点,要求将此结点从链表中删除。

思路:

  比较巧妙,将当前结点伪装成下一个结点,然后将下一个结点删除即可。

C++:

1 /** 2  * Definition for singly-linked list. 3  * struct ListNode { 4  *     int val; 5  *     ListNode *next; 6  *     ListNode(int x) : val(x), next(NULL) {} 7  * }; 8  */ 9 class Solution {10 public:11     void deleteNode(ListNode* node) {12         13         if(node == 0)14             return ;15         16         node->val  = node->next->val;17         18         ListNode *del = node->next;19         node->next = del->next;20         21         delete del;22     }23 };

 

转载于:https://www.cnblogs.com/tjuloading/p/4648652.html

你可能感兴趣的文章
通过向viewpage中添加listview来完成滑动效果(类似于qq滑动界面) android开发
查看>>
MyEclipse使用总结——MyEclipse去除网上复制下来的来代码带有的行号
查看>>
java学习笔记-设计模式20(备忘录模式)
查看>>
*Hdu 1026-Ignatius and the Princess I
查看>>
447. Number of Boomerangs
查看>>
【bzoj1598/Usaco2008 Mar】牛跑步——A*
查看>>
威斯敏斯特教堂(西敏寺)墓碑上的话(WestMinster Abbey,When I was young and free...,修身齐家治国平天下)...
查看>>
IDEA激活
查看>>
轻型的接口访问频率限制服务模型的设计与实现【转】
查看>>
创建多线程的第一种方式——创建Thread子类和重写run方法
查看>>
Bug管理工具的使用介绍(Bugger 2016)
查看>>
Redis Win平台安装
查看>>
Java EE学习笔记(二)
查看>>
Python接口自动化基础---get请求
查看>>
HDU 4442 Physical Examination
查看>>
个性化推荐系统最近一些复盘以及探索
查看>>
使用NEWSEQUENTIALID解决GUID聚集索引问题
查看>>
FILESTREAM
查看>>
如何获取用户的地理位置-浏览器地理位置(Geolocation)API 简介
查看>>
amazeui学习笔记--css(常用组件9)--导航nav
查看>>