首页 > 其他分享 >HJ48 从单向链表中删除指定值的节点

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

时间:2023-07-21 17:11:16浏览次数:44  
标签:head cur val int 单向 结点 next 链表 HJ48

1. 题目

读题

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

 

考查点

 

这道题的考查点是单向链表的删除操作,主要是考察你能否掌握以下几个方面:

  • 如何遍历链表,找到要删除的节点或其前驱节点。
  • 如何修改节点的指针域,使其跳过要删除的节点。
  • 如何释放要删除的节点的内存空间,防止内存泄漏。
  • 如何处理特殊情况,比如要删除的节点是头节点或尾节点,或者链表为空或不存在该节点。

 

2. 解法

思路

 

代码逻辑

 

具体实现

import java.util.Scanner;

class Node {
    int val;
    Node next;
    Node(int val) {
        this.val = val;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt(); // 链表结点个数
            int headVal = sc.nextInt(); // 头结点的值
            Node head = new Node(headVal); // 创建头结点
            Node cur = head; // 当前结点指针
            for (int i = 0; i < n - 1; i++) {
                int val = sc.nextInt(); // 要插入的结点值
                int pos = sc.nextInt(); // 要插入的位置
                Node node = new Node(val); // 创建新结点
                cur = head; // 从头开始遍历
                while (cur != null) {
                    if (cur.val == pos) { // 找到要插入的位置
                        node.next = cur.next; // 新结点指向原来的后继结点
                        cur.next = node; // 原来的结点指向新结点
                        break;
                    }
                    cur = cur.next; // 移动指针
                }
            }
            int delVal = sc.nextInt(); // 要删除的结点值
            deleteNode(head, delVal); // 删除操作
        }
        sc.close();
    }

    public static void deleteNode(Node head, int delVal) {
        if (head == null) return; // 空链表直接返回
        if (head.val == delVal) { // 如果要删除头结点
            head = head.next; // 头结点指向下一个结点
        } else { // 如果要删除非头结点
            Node pre = head; // 前驱结点指针
            Node cur = head.next; // 当前结点指针
            while (cur != null) {
                if (cur.val == delVal) { // 找到要删除的结点
                    pre.next = cur.next; // 前驱结点指向后继结点
                    break;
                }
                pre = pre.next; // 移动前驱指针
                cur = cur.next; // 移动当前指针
            }
        }
        printList(head); // 打印链表
    }

    public static void printList(Node head) {
        Node cur = head; // 当前结点指针
        while (cur != null) {
            System.out.print(cur.val + " "); // 打印当前结点值
            cur = cur.next; // 移动指针
        }
        System.out.println(); // 换行
    }
}

 

自行实现

public class HJ048 {
static class ListNode {
public int val;
public ListNode next;

public ListNode(int val) {
this.val = val;
}

public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int headVal = sc.nextInt();
ListNode head = new ListNode(headVal);
for (int i = 0; i < n-1; i++) {
int val = sc.nextInt();
int preVal = sc.nextInt();
ListNode cur = head;
while (cur != null) {
if (cur.val == preVal) {
ListNode next = cur.next;
cur.next = new ListNode(val, next);
break;
}
cur = cur.next;
}
}
int delVal = sc.nextInt();
ListNode ans = deleteNode(head, delVal);
printLinklist(ans);
}

public static ListNode deleteNode(ListNode head, int val) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode curr = head;
ListNode pre = dummy;
while (curr != null) {
if (curr.val == val) {
pre.next = curr.next;
return dummy.next;
}
pre = curr;
curr = curr.next;
}
return dummy.next;
}

public static void printLinklist(ListNode head) {

while (head != null) {
System.out.print(head.val + " ");
head = head.next;
}
}
}

3. 总结

标签:head,cur,val,int,单向,结点,next,链表,HJ48
From: https://www.cnblogs.com/shoshana-kong/p/17548668.html

相关文章

  • 剑指 Offer 18. 删除链表的节点
    题目:(有改动和陷阱,不可以使用delete否则报错!!)classSolution{public:ListNode*deleteNode(ListNode*head,intval){ListNode*fhead=newListNode(0);#设置虚拟头节点fhead->next=head;ListNode*cur=fhead;while(cur-......
  • 剑指 Offer 24. 反转链表
    题目:/***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode(intx):val(x),next(NULL){}*};*/classSolution{public:ListNode*reverseList(ListNode*head){ListNode*temp......
  • 剑指 Offer 06. 从尾到头打印链表
    题目:/***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode(intx):val(x),next(NULL){}*};*/classSolution{public:vector<int>reversePrint(ListNode*head){vecto......
  • 上班摸鱼刷算法-Java-hot100-[160]相交链表
    publicclassSolution{publicListNodegetIntersectionNode(ListNodeheadA,ListNodeheadB){if(headA==null||headB==null){returnnull;}ListNodepA=headA;ListNodepB=headB;while(pA......
  • 上班摸鱼刷算法-Java-hot100-[21]合并两个有序链表
    //将一个链表插入到另一个链表中classSolution{publicListNodemergeTwoLists(ListNodelist1,ListNodelist2){if(list1==null){returnlist2;}if(list2==null){returnlist1;}retur......
  • 数据结构练习笔记——删除单链表中某区间的数
    删除单链表中某区间的数【问题描述】已知某带头结点的单链表中存放着若干整数,请删除该单链表中元素在[x,y]之间的所有结点,要求算法的时间复杂度为O(n),空间复杂度为O(1)。【输入形式】​ 第一行:单链表中元素个数m​ 第二行:单链表中的m个整数​ 第三行:要删除的元素值所在区......
  • [刷题记录Day4]Leetcode链表专题
    No.1题目两两交换链表中的节点思路模拟类型题目两个节点前后交换,同时记住原来的下一个节点虚拟头节点代码public ListNode swapPairs(ListNode head) { ListNode dummyHead = new ListNode(-1, head); ListNode cur = dummyHead; while (cur.next != ......
  • 2 链表
    #链表##1链表基础理论什么是链表,链表是一种通过指针串联在一起的线性结构,每一个节点由两部分组成,一个是数据域一个是指针域(存放指向下一个节点的指针),最后一个节点的指针域指向null(空指针的意思)。链表的入口节点称为链表的头结点也就是head。-单链表![img](https://img......
  • 剑指offer--链表
    第6题:链表中倒数最后k个结点题目描述输入一个长度为n的链表,设链表中的元素的值为\(a_i\),返回该链表中的第k个结点。如果该链表长度小于\(k\),请返回一个长度为0的链表思路双指针step1:准备一个快指针,从链表头开始,在链表上先走k步。step2:准备慢指针指向原始链表头,代......
  • 单链表快速排序
    title:单链表快速排序date:2023-07-1809:06:37tags:-c/c++categories:-算法-笔试top:单链表快速排序题目来自acwing题目(点击跳转)给定一个单链表,请使用快速排序算法对其排序。要求:期望平均时间复杂度为O(nlogn),期望额外空间复杂度为O(logn)。思考题:如果只能......