lotus

贵有恒何必三更眠五更起 最无益只怕一日曝十日寒

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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. 总结

posted on 2023-07-21 17:05  白露~  阅读(45)  评论(0编辑  收藏  举报