24.从单向链表中删除指定值的节点

题目描述

输入一个单向链表和一个节点的值,从单向链表中删除等于该值的节点,删除后如果链表中无节点则返回空指针。

链表结点定义如下:

struct ListNode

{

      int       m_nKey;

      ListNode* m_pNext;

};

详细描述:

本题为考察链表的插入和删除知识。

链表的值不能重复

构造过程,例如

1 -> 2

3 -> 2

5 -> 1

4 -> 5

7 -> 2

最后的链表的顺序为 2 7 3 1 5 4 

删除 结点 2 

则结果为 7 3 1 5 4 


import java.util.Scanner;
import java.util.Stack;
 
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext())
        {
            //输入节点数
            int nodeNum = in.nextInt();
            //输入头节点的值
            int headVal = in.nextInt();
            //构造头节点
            Node pHead = new Node(headVal);
            for(int i = 0;i < nodeNum - 1;i ++)
            {
                //插入节点值
                int nextVal = in.nextInt();
                //前一节点值
                int preVal = in.nextInt();
                //插入节点
                insert(pHead,preVal,nextVal);
            }
            int deleteVal = in.nextInt();
            pHead = removeNode(pHead, deleteVal);
            while(pHead.next != null)
            {
                System.out.print(pHead.val + " ");
                pHead = pHead.next;
            }
            System.out.println(pHead.val + " ");
        }
    }
    /*
     参数 pHead 头节点
     preVal 上一节点值
     nextVal 插入节点值 
    */
    public static void insert(Node pHead,int preVal,int nextVal)
    {
        Node pNext = new Node(nextVal);
        Node pCurrent = pHead;
        while(pCurrent != null)
        {
            if(pCurrent.val == preVal)
            {
                pNext.next = pCurrent.next;
                pCurrent.next = pNext;
                break;
            }
            pCurrent = pCurrent.next;
        }
    }
    public static Node removeNode(Node pHead,int val)
    {
        Stack<Node> stack = new Stack<Node>();
        Node preNode = pHead;
        while(pHead != null)
        {
            if(pHead.val != val)
            {
                stack.push(pHead);
                
            } 
            pHead = pHead.next;
        }
        while(!stack.isEmpty())
        {
            stack.peek().next = pHead;
            pHead = stack.pop();
        }
        return pHead;
    }
}
class Node
{
    public int val;
    public Node next;
    public Node(int val)
    {
        this.val = val;
    }
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python,可以通过定义节点类和链表类来实现单向链表的操作。删除指定节点可以通过遍历链表,找到为目标节点,然后连接其前驱节点和后继节点来实现节点删除。 具体的实现步骤如下: 1. 定义节点类:节点类需要包含两个属性,一个是节点,另一个是指向下一个节点的指针。同时要定义一个初始化节点的方法。 ```python class Node: def __init__(self, val): self.val = val self.next = None ``` 2. 定义链表类:链表类需要包含至少一个属性,即头节点。其包含插入和删除节点的方法。 ```python class LinkedList: def __init__(self): self.head = None def insert(self, val): node = Node(val) if self.head is None: self.head = node else: node.next = self.head self.head = node def remove(self, val): if self.head is None: return if self.head.val == val: self.head = self.head.next return prev = self.head curr = self.head.next while curr is not None: if curr.val == val: prev.next = curr.next return prev = curr curr = curr.next ``` 3. 删除指定节点:在链表类实现remove方法,遍历链表,找到为目标节点,然后连接其前驱节点和后继节点来实现节点删除。 具体如上面代码所示,当链表为空或者头节点等于目标时,可以直接删除节点。否则需要遍历链表,找到目标节点的前驱节点和后继节点,然后连接前驱节点和后继节点即可。 这就是Python单向链表删除指定节点的实现方式,可以应用于一些需要动态更新数据结构的场景
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值