LintCode: Delete Node in the Middle of Singly Linked List

删除链表中节点的C++实现
本文介绍了一种删除链表中指定节点的方法,通过将待删节点的值替换为下一个节点的值,并调整指针指向来完成删除操作。提供了一个简单的C++示例代码。

开始没看懂题目的意思,以为是输入一个单链表,删掉链表中间的那个节点。

实际的意思是,传入的参数就是待删节点,所以只要把当前节点指向下一个节点就可以了。

C++

复制代码
 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         node->val = node->next->val;
22         node->next = node->next->next;
23     }
24 };
复制代码

 


本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/5012617.html,如需转载请自行联系原作者

1 Traversing a singly linked list with a dummy header 分数 20 全屏浏览 切换布局 作者 伍建全 单位 重庆科技大学 In this problem you will Implement a function to traverse a singly linked list with a dummy header and output the data field of each node. Structure description: The node structure is shown below: typedef struct ListNode { int data; struct ListNode *next; } node; typedef node* position; typedef position List; Function definition: void traversal(List L); The parameter L is a pointer to the dummy header. The function outputs the data field of each node as it traverses the singly linked list. Test program example: #include <stdio.h> #include <stdlib.h> typedef struct ListNode { int data; struct ListNode *next; }node; typedef node* position; typedef position List; void traversal(List L); // The questioner has implemented the createList function. // This function reads a series of positive integers separated by spaces // and inserts them into a linked list using the head insertion method. // Entering -1 indicates the end of input. // creatgeList函数由题目提供,不需要在本题的答案中实现 List createList(); int main(void) { List L = createList(); traversal(L); return 0; } /* Please write the answer here */ Input Specification: A series of positive integers separated by spaces. Entering -1 indicates the end of input. Output Specification: Output the data field of each node in the singly linked list. After each number, output a space.(每个整数后输出1个空格) Sample Input : 9 5 7 2 -1 Sample Output : 2 7 5 9 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值