热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在单链表中仅提供指向要删除的节点的指针,如何删除它?

在单链表中仅提供指向要删除的节点的指针,如何删除它?原文:htt

在单链表中仅提供指向要删除的节点的指针,如何删除它?

原文:https://www.geeksforgeeks.org/in-a-linked-list-given-only-a-pointer-to-a-node-to-be-deleted-in-a-singly-linked-list-how-do-you-delete-it/

简单解决方案是遍历链表,直到找到要删除的节点。 但是此解决方案需要指向头节点的指针,该指针与问题陈述相矛盾。

快速解决方案是将数据从下一个节点复制到要删除的节点,然后删除下一个节点。 像这样:

struct Node *temp = node_ptr->next;
node_ptr->data = temp->data;
node_ptr->next = temp->next;
free(temp);

下面是上述代码的实现:

C++

// C++ program to del the node
// in which only a single pointer
// is known pointing to that node
#include
#include
using namespace std;
/* Link list node */
class Node {
public:
    int data;
    Node* next;
};
/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(Node** head_ref, int new_data)
{
    /* allocate node */
    Node* new_node = new Node();
    /* put in the data */
    new_node->data = new_data;
    /* link the old list off the new node */
    new_node->next = (*head_ref);
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
void printList(Node* head)
{
    Node* temp = head;
    while (temp != NULL) {
        cout <data <<" ";
        temp = temp->next;
    }
}
void deleteNode(Node* node_ptr)
{
    // If the node to be deleted is the 
    // last node of linked list
    if (node_ptr->next == NULL)
    {
        free(node_ptr);
        // this will simply make the node_ptr NULL.
        return;
    }
    // if node to be deleted is the first or 
    // any node in between the linked list.
    Node* temp = node_ptr->next;
    node_ptr->data = temp->data;
    node_ptr->next = temp->next;
    free(temp);
}
// Driver code
int main()
{
    /* Start with the empty list */
    Node* head = NULL;
    /* Use push() to construct below list
    1->12->1->4->1 */
    push(&head, 1);
    push(&head, 4);
    push(&head, 1);
    push(&head, 12);
    push(&head, 1);
    cout <<"Before deleting \n";
    printList(head);
    /* I m deleting the head itself.
        You can check for more cases */
    deleteNode(head);
    cout <<"\nAfter deleting \n";
    printList(head);
    return 0;
}
// This code is contributed by rathbhupendra

C

// C++ program to del the node
// in which only a single pointer
// is known pointing to that node
#include
#include
#include
/* Link list node */
struct Node {
    int data;
    struct Node* next;
};
/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct Node* new_node
        = (struct Node*)malloc(sizeof(struct Node));
    /* put in the data  */
    new_node->data = new_data;
    /* link the old list off the new node */
    new_node->next = (*head_ref);
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
void printList(struct Node* head)
{
    struct Node* temp = head;
    while (temp != NULL) {
        printf("%d  ", temp->data);
        temp = temp->next;
    }
}
void deleteNode(struct Node* node_ptr)
{
    // If the node to be deleted is the last 
    // node of linked list
    if (node_ptr->next == NULL)
    {
        free(node_ptr);
        // this will simply make the node_ptr NULL.
        return;
    }
    struct Node* temp = node_ptr->next;
    node_ptr->data = temp->data;
    node_ptr->next = temp->next;
    free(temp);
}
// Driver code 
int main()
{
    /* Start with the empty list */
    struct Node* head = NULL;
    /* Use push() to construct below list
    1->12->1->4->1  */
    push(&head, 1);
    push(&head, 4);
    push(&head, 1);
    push(&head, 12);
    push(&head, 1);
    printf("\n Before deleting \n");
    printList(head);
    /* I m deleting the head itself.
        You can check for more cases */
    deleteNode(head);
    printf("\n After deleting \n");
    printList(head);
    getchar();
}

Java

// Java program to del the node in 
// which only a single pointer is 
// known pointing to that node
class LinkedList {
    static Node head;
    static class Node {
        int data;
        Node next;
        Node(int d)
        {
            data = d;
            next = null;
        }
    }
    void printList(Node node)
    {
        while (node != null) {
            System.out.print(node.data + " ");
            node = node.next;
        }
    }
    void deleteNode(Node node)
    {
        Node temp = node.next;
        node.data = temp.data;
        node.next = temp.next;
        System.gc();
    }
    // Driver code
    public static void main(String[] args)
    {
        LinkedList list = new LinkedList();
        list.head = new Node(1);
        list.head.next = new Node(12);
        list.head.next.next = new Node(1);
        list.head.next.next.next = new Node(4);
        list.head.next.next.next.next = new Node(1);
        System.out.println("Before Deleting ");
        list.printList(head);
        /* I m deleting the head itself.
         You can check for more cases */
        list.deleteNode(head);
        System.out.println("");
        System.out.println("After deleting ");
        list.printList(head);
    }
}
// This code has been contributed by Mayank Jaiswal

Python3

# A python3 program to delete
# the node in which only a single pointer
# is known pointing to that node
# Linked list node
class Node():
    def __init__(self):
        self.data = None
        self.next = None
# Given a reference (pointer to pointer)
# to the head of a list and an int,
# push a new node on the front of the list
def push(head_ref, new_data):
    # allocate node
    new_node = Node()
    # put in the data
    new_node.data = new_data
    # link the old list off the new node
    new_node.next = head_ref
    # move the head to point to the new node
    head_ref = new_node
    return head_ref
def printList(head):
    temp = head
    while(temp != None):
        print(temp.data, end=' ')
        temp = temp.next
def deleteNode(node_ptr):
    temp = node_ptr.next
    node_ptr.data = temp.data
    node_ptr.next = temp.next
# Driver code
if __name__ == '__main__':
    # Start with the empty list
    head = None
    # Use push() to construct below list
    # 1->12->1->4->1
    head = push(head, 1)
    head = push(head, 4)
    head = push(head, 1)
    head = push(head, 12)
    head = push(head, 1)
    print("Before deleting ")
    printList(head)
    # I'm deleting the head itself.
    # You can check for more cases
    deleteNode(head)
    print("\nAfter deleting")
    printList(head)
# This code is contributed by Yashyasvi Agarwal

C

// C# program to del the node in
// which only a single pointer is
// known pointing to that node
using System;
public class LinkedList {
    Node head;
    public class Node {
        public int data;
        public Node next;
        public Node(int d)
        {
            data = d;
            next = null;
        }
    }
    void printList(Node node)
    {
        while (node != null) {
            Console.Write(node.data + " ");
            node = node.next;
        }
    }
    void deleteNode(Node node)
    {
        Node temp = node.next;
        node.data = temp.data;
        node.next = temp.next;
    }
    // Driver code
    public static void Main()
    {
        LinkedList list = new LinkedList();
        list.head = new Node(1);
        list.head.next = new Node(12);
        list.head.next.next = new Node(1);
        list.head.next.next.next = new Node(4);
        list.head.next.next.next.next = new Node(1);
        Console.WriteLine("Before Deleting ");
        list.printList(list.head);
        /* I m deleting the head itself.
        You can check for more cases */
        list.deleteNode(list.head);
        Console.WriteLine("");
        Console.WriteLine("After deleting ");
        list.printList(list.head);
    }
}
/* This code contributed by PrinciRaj1992 */

输出

Before deleting
1 12 1 4 1
After deleting
12 1 4 1

为了使该解决方案有效,我们可以将末端节点标记为虚拟节点。 但是使用此函数的程序/函数也应进行修改。

对于双链表,请尝试解决此问题。


推荐阅读
  • 深入理解Redis的数据结构与对象系统
    本文详细探讨了Redis中的数据结构和对象系统的实现,包括字符串、列表、集合、哈希表和有序集合等五种核心对象类型,以及它们所使用的底层数据结构。通过分析源码和相关文献,帮助读者更好地理解Redis的设计原理。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 数据库内核开发入门 | 搭建研发环境的初步指南
    本课程将带你从零开始,逐步掌握数据库内核开发的基础知识和实践技能,重点介绍如何搭建OceanBase的开发环境。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • Codeforces Round #566 (Div. 2) A~F个人题解
    Dashboard-CodeforcesRound#566(Div.2)-CodeforcesA.FillingShapes题意:给你一个的表格,你 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 题目Link题目学习link1题目学习link2题目学习link3%%%受益匪浅!-----&# ... [详细]
  • 本文详细探讨了VxWorks操作系统中双向链表和环形缓冲区的实现原理及使用方法,通过具体示例代码加深理解。 ... [详细]
  • 本题涉及一棵由N个节点组成的树(共有N-1条边),初始时所有节点均为白色。题目要求处理两种操作:一是改变某个节点的颜色(从白变黑或从黑变白);二是查询从根节点到指定节点路径上的第一个黑色节点,若无则输出-1。 ... [详细]
  • Linux设备驱动程序:异步时间操作与调度机制
    本文介绍了Linux内核中的几种异步延迟操作方法,包括内核定时器、tasklet机制和工作队列。这些机制允许在未来的某个时间点执行任务,而无需阻塞当前线程,从而提高系统的响应性和效率。 ... [详细]
  • 基于KVM的SRIOV直通配置及性能测试
    SRIOV介绍、VF直通配置,以及包转发率性能测试小慢哥的原创文章,欢迎转载目录?1.SRIOV介绍?2.环境说明?3.开启SRIOV?4.生成VF?5.VF ... [详细]
  • 本次考试于2016年10月25日上午7:50至11:15举行,主要涉及数学专题,特别是斐波那契数列的性质及其在编程中的应用。本文将详细解析考试中的题目,并提供解题思路和代码实现。 ... [详细]
  • 作者:守望者1028链接:https:www.nowcoder.comdiscuss55353来源:牛客网面试高频题:校招过程中参考过牛客诸位大佬的面经,但是具体哪一块是参考谁的我 ... [详细]
author-avatar
_ZY寶貝_
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有