删除链表节点算法思路

问题重述:给定单向链表的一个节点指针,定义一个函数在O(1)时间删除该结点。

前提:假设链表一定存在,并且该节点一定不是尾节点。

 

思路:

No1:

正常的思路是利用该节点前驱节点的指针,将该节点的前驱节点的next指向当前节点的后继,如果前驱节点的指针为 *p,则接下来的操作为: p -> next = p -> next ->next;

No2:

或者利用两个指针:*p 前驱结点的指针  *q 当前节点的指针

通过:p -> next = q -> next

与思路一基本类似

No3:

本题的要求与上述两种方法不太相符,首先只给了当前节点的一个指针,要是通过遍历找到它的前驱节点需要的时间复杂度为O(n),本题要求时间复杂度为O(1).所以就不能使用前驱节点,此时就要换一种思路。题中说该节点一定不是尾节点,所以当前节点的后驱节点一定不为空。所以就考虑用当前节点的后驱节点覆盖当前节点,然后删除原来的后驱节点。

操作:当前节点的指针 *p

p -> val = p -> next -> val;

p -> next = p -> next -> next;

具体代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) {
        node -> val = node -> next -> val;
        node -> next = node -> next -> next;
    }
};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
动态链表是一种常见的数据结构,它的基本操作包括插入、删除、查找等。下面是一个简单的动态链表生成算法思路,使用C语言实现。 1. 定义一个结构体来表示链表中的一个节点,包含数据和指向下一个节点的指针: ```c struct Node { int data; struct Node* next; }; ``` 2. 定义一个指向链表结点的指针,并初始化为NULL: ```c struct Node* head = NULL; ``` 3. 定义一个循环,用于输入数据并插入到链表中。循环的退出条件可以是输入一个特定的值,比如-1: ```c while (1) { int value; printf("Enter a value (-1 to exit): "); scanf("%d", &value); if (value == -1) { break; } // create a new node struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; newNode->next = NULL; // insert the new node into the list if (head == NULL) { head = newNode; } else { struct Node* current = head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } ``` 4. 定义一个循环,用于遍历链表并输出每个节点的值: ```c struct Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } ``` 完整的代码如下: ```c #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; int main() { // initialize the list with NULL struct Node* head = NULL; // insert values into the list while (1) { int value; printf("Enter a value (-1 to exit): "); scanf("%d", &value); if (value == -1) { break; } // create a new node struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; newNode->next = NULL; // insert the new node into the list if (head == NULL) { head = newNode; } else { struct Node* current = head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } // print the values in the list struct Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } // free the memory allocated for the list current = head; while (current != NULL) { struct Node* temp = current; current = current->next; free(temp); } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bow.贾斯汀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值