LeetCode //C - 237. Delete Node in a Linked List

237. Delete Node in a Linked List

There is a singly-linked list head and we want to delete a node node in it.

You are given the node to be deleted node. You will not be given access to the first node of head.

All the values of the linked list are unique, and it is guaranteed that the given node node is not the last node in the linked list.

Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:

  • The value of the given node should not exist in the linked list.
  • The number of nodes in the linked list should decrease by one.
  • All the values before node should be in the same order.
  • All the values after node should be in the same order.

Custom testing:

  • For the input, you should provide the entire linked list head and the node to be given node. node should not be the last node of the list and should be an actual node in the list.
  • We will build the linked list and pass the node to your function.
  • The output will be the entire list after calling your function.
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.

Constraints:
  • The number of the nodes in the given list is in the range 2, 1000.
  • -1000 <= Node.val <= 1000
  • The value of each node in the list is unique.
  • The node to be deleted is in the list and is not a tail node.

From: LeetCode

Link: 237. Delete Node in a Linked List


Solution:

Ideas:
  • Check for edge cases: The function first checks if the node to be deleted is NULL or if it is the last node. If either condition is true, the function returns immediately since it is not possible to delete the last node or a NULL node with this approach.
  • Copy the next node's value: The value of the next node is copied to the given node.
  • Link to the next of next node: The next pointer of the given node is updated to skip the next node, effectively linking to the node after the next node.
  • Free the next node: The memory of the next node (which is now redundant) is freed.
Code:
c 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
void deleteNode(struct ListNode* node) {
    if (node == NULL || node->next == NULL) {
        return; // Cannot delete if the node is NULL or it is the last node
    }
    
    struct ListNode* nextNode = node->next;
    node->val = nextNode->val;
    node->next = nextNode->next;
    free(nextNode); // Free the memory of the next node
}
相关推荐
潜创微科技11 分钟前
IT6520:USB-C 转 MIPI 高集成控制器,DP 1.4a 转 MIPI 单芯片搞定 4K120 显示
c语言·开发语言·低延迟·掌机·联阳
wzdark16 分钟前
多维数组在算法设计中的存储映射问题4
算法
Phil3231 小时前
多智能体不是越多越好:Google《Towards a Science of Scaling Agent Systems》论文深度解读
算法
6Hzlia1 小时前
【Classic 150 刷题计划】 LeetCode 26. 删除有序数组中的重复项 | C++ 快慢双指针经典模板
c++·算法·leetcode
白色的北极熊2 小时前
字符转 ASCII 码
c语言
huang5791472 小时前
基于滑动窗口的流式数据算法优化思路3
算法
Ulyanov2 小时前
AudioVision Pro:基于 PySide6 + sounddevice 的实时音频可视化播放器设计
python·算法·音视频
土司大王2 小时前
LeetCode hot100——74.搜索二维矩阵:Java 二分模板
java·算法·leetcode
tyler_download2 小时前
揉扁搓圆transformer架构:NAG优化器算法详解
深度学习·算法·transformer
weixin199701080162 小时前
[特殊字符]《京东POP二手品类对接:B2C订单模型 vs 二手C2C属性的字段转换难题》(附Python源码)
前端·python·算法