热门标签 | 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

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

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


推荐阅读
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • 题目Link题目学习link1题目学习link2题目学习link3%%%受益匪浅!-----&# ... [详细]
  • 本文详细介绍了中央电视台电影频道的节目预告,并通过专业工具分析了其加载方式,确保用户能够获取最准确的电视节目信息。 ... [详细]
  • Codeforces Round #566 (Div. 2) A~F个人题解
    Dashboard-CodeforcesRound#566(Div.2)-CodeforcesA.FillingShapes题意:给你一个的表格,你 ... [详细]
  • 深入理解Redis的数据结构与对象系统
    本文详细探讨了Redis中的数据结构和对象系统的实现,包括字符串、列表、集合、哈希表和有序集合等五种核心对象类型,以及它们所使用的底层数据结构。通过分析源码和相关文献,帮助读者更好地理解Redis的设计原理。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 本文详细介绍了Java中org.w3c.dom.Text类的splitText()方法,通过多个代码示例展示了其实际应用。该方法用于将文本节点在指定位置拆分为两个节点,并保持在文档树中。 ... [详细]
  • 本文详细介绍了Java中的访问器(getter)和修改器(setter),探讨了它们在保护数据完整性、增强代码可维护性方面的重要作用。通过具体示例,展示了如何正确使用这些方法来控制类属性的访问和更新。 ... [详细]
  • 本文详细探讨了JDBC(Java数据库连接)的内部机制,重点分析其作为服务提供者接口(SPI)框架的应用。通过类图和代码示例,展示了JDBC如何注册驱动程序、建立数据库连接以及执行SQL查询的过程。 ... [详细]
  • 实体映射最强工具类:MapStruct真香 ... [详细]
  • 本文探讨了在Java中实现系统托盘最小化的两种方法:使用SWT库和JDK6自带的功能。通过这两种方式,开发者可以创建跨平台的应用程序,使窗口能够最小化到系统托盘,并提供丰富的交互功能。 ... [详细]
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社区 版权所有