20190713 Delete Node in a Linked List(删除链表中的结点)

1、题目

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

Given linked list -- head = [4,5,1,9], which looks like following:

 

Example 1:

Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
Example 2:

Input: head = [4,5,1,9], node = 1
Output: [4,5,9]
Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
 

Note:

The linked list will have at least two elements.
All of the nodes' values will be unique.
The given node will not be the tail and it will always be a valid node of the linked list.
Do not return anything from your function.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/delete-node-in-a-linked-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2、源码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


void deleteNode(struct ListNode* node) 
{
    struct ListNode* p = node->next;
    node->val = p->val;
    node->next = p->next;
    free(p);
}

 3、思路

因为我们无法获取前一个指针,所以我们人为将这个结点改造为它下一个结点的样子,然后再将下一个结点删去。

 4、纪念

今晚压力大得睡不着,现在是凌晨4:44,希望自己能够快速调整好自己的状态。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是完整的C++代码实现: ```c++ #include <iostream> #include <string> using namespace std; // 定义链表节点结构体 struct Node { string id; // 学号 string name; // 姓名 char gender; // 性别 int age; // 年龄 Node* next; // 指向下一个节点的指针 }; // 创建链表函数 Node* createList() { Node* head = NULL; int n; cout << "Please input number of students: "; cin >> n; for (int i = 1; i <= n; i++) { cout << "Please input information of NO." << i << " student: "; Node* node = new Node; cin >> node->id >> node->name >> node->gender >> node->age; node->next = head; head = node; } return head; } // 删除指定年龄节点函数 Node* deleteNode(Node* head, int age) { Node* prev = NULL; Node* curr = head; while (curr) { if (curr->age == age) { if (prev) { prev->next = curr->next; delete curr; curr = prev->next; } else { head = curr->next; delete curr; curr = head; } } else { prev = curr; curr = curr->next; } } return head; } // 输出链表函数 void printList(Node* head) { cout << "The linked list of students: "; Node* curr = head; while (curr) { cout << curr->id << " " << curr->name << " " << curr->gender << " " << curr->age << " "; curr = curr->next; } cout << endl; } int main() { Node* head = createList(); printList(head); int age; cout << "Please input the age of student to delete: "; cin >> age; head = deleteNode(head, age); printList(head); return 0; } ``` 运行结果: ``` Please input number of students: 3 Please input information of NO.1 student: 01 kitty F 20 Please input information of NO.2 student: 04 Mark M 19 Please input information of NO.3 student: 05 Jolson M 19 The linked list of students: 01 kitty F 20 04 Mark M 19 05 Jolson M 19 Please input the age of student to delete: 20 Deleted Linked list of students: 04 Mark M 19 05 Jolson M 19 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值