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.

题目意思:

  写一个函数删除单链表中的某个结点,函数只给出了删除的结点

解题思路:

  只需要将删除的结点与其下一个结点交换值即可,同时调整好链表指针

源代码:

 class Solution {
public:
void deleteNode(ListNode* node) {
if( node->next == NULL){
delete node;
node = NULL;
}
swap(node->val, node->next->val);
ListNode* nextNode = node->next;
node->next = nextNode->next;
delete nextNode;
}
};

Leetcode Delete Node in a Linked List的更多相关文章

  1. [LeetCode] Delete Node in a Linked List 删除链表的节点

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

  2. LeetCode——Delete Node in a Linked List

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

  3. LeetCode Delete Node in a Linked List (删除链表中的元素)

    题意:给一个将要删除的位置的指针,要删除掉该元素.被删元素不会是链尾(不可能删得掉). 思路:将要找到前面的指针是不可能了,但是可以将后面的元素往前移1位,再删除最后一个元素. /** * Defin ...

  4. [LeetCode] 237. Delete Node in a Linked List 解题思路

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

  5. 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements

    237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...

  6. LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

  7. [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点

    2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...

  8. LeetCode_237. Delete Node in a Linked List

    237. Delete Node in a Linked List Easy Write a function to delete a node (except the tail) in a sing ...

  9. 【4_237】Delete Node in a Linked List

    Delete Node in a Linked List Total Accepted: 48121 Total Submissions: 109297 Difficulty: Easy Write ...

随机推荐

  1. C# HttpWebReqeust和HttpWebResponse发送请求

    var request = (HttpWebRequest)WebRequest.Create("URL"); var data = Encoding.UTF8.GetBytes( ...

  2. 基于vw的响应式排版布局

    html{ /*iphone6的375px尺寸作为16px基准,600px时字体大小为18px*/ font-size: calc(100% + 2 * (100vw - 375px) / 225); ...

  3. ubuntu

    mongoChef: http://3t.io/mongochef/download/core/platform/#tab-id-3 背景色改成豆沙绿: /usr/share/themes/Ambia ...

  4. iOS sqlite数据库图像化查看

    问题描述:在xocde上用sqlite数据库的时候,因为没有图形化界面,有些时候很难看出自己设计的数据库是否有问题,比如我刚上手sqlite数据库设计id为自增长时,很自然的用了identify(1, ...

  5. JavaBean的用法

    JavaBean是一个可重复使用的软件组件,是用Java语言编写的.遵循一定标准的类. JavaBean是Java Web的重要组件,它封装了数据和操作的功能类,供JSP和Servlet调用,完成数据 ...

  6. thinkpad W500S 如何换键盘?

    tHINKPAD的笔记本拆装有,123456789... 至少5种以上了,一般键盘去下都是边上撬就去下 来了.今天拆换W550S键盘就遇到劲敌了.拼了 老劲也去不下,冬天背上都冒汗(屋子热的吧). 终 ...

  7. Andrew Ng在coursera上的ML课程_知识点笔记_(1)

    1.Feature Scaling(特征缩放): 如上图所示,x1是房屋面积,x2是房间个数,若不进行特征缩放,则代价函数J的曲线近似为一个瘦长的椭圆(我暂时这么理解,θ1和θ2分别是x1和x2的权值 ...

  8. wps恢复经典模式

    经典模式 情况一: 恢复的方法:点击红色区域 情况二: 恢复的方法:点击红色区域

  9. 重装系统后如何删除系统自带的office2003

    背景:操作系统重装后,系统自带的office2003版本太低不好使用,需要安装更高版本的如2010,但是卸载时提示“无法打开此修补程序包……”,从而无法卸载.经网上查询,采用以下解决办法: 在注册表中 ...

  10. 那些年,坑死自己的事之fread/fwrite

    今天继续看牛人做过的东西,这个小程序并不大,加上相当多的注释行,才5000多行.这个小程序是在linux下实现的,之前自己也一直用vi来看并加以更加详细的注释,但是效率实在太低.于是将其转移到wind ...