Find the first node with a certain value in the linked list, delete the node, and print the linked list from beginning to end

1. Find the first node with a certain value in the linked list and delete the node

  • Method 1: Analysis: There is a common practice for this question, which is to traverse the nodes one by one, and if the value of the node is equal to the given value, delete the node.

But for deleting a node, for an ordinary linked list, if it is the head node, fortunately, head=head.next directly, and then traverse backward in turn. There is an extreme situation here. If the head node has always been the node that needs to be deleted, then this deletion also needs to do a while loop.

In addition, for the deletion of ordinary nodes, we usually only need to point the next of the previous node of the current node to the next of the current node. Therefore, we need two pointers during the traversal process, one to the current node and one to The node before the current node.

  • Method 2: Instead of directly operating the linked list to delete, it made a trick. Using the stack data structure, the data nodes that are not equal to the specified value in the linked list are sequentially pushed into the stack. After the end, the end to the beginning is constructed. Linked list, return to the head node.
public static Node removeElement(Node head, int val) {
    
    
        //判断头节点是否是删除的元素
        while (head != null) {
    
    
            if ((int) head.date != val) {
    
    
                break;
            }
            head = head.next;
            return head;
        }

        Node cur = head;
        Node pre = head;
        while (cur != null) {
    
    
            //第一次循环后cur和pre相差一个指针节点。
            if ((int) cur.date == val) {
    
    
                pre.next = cur.next;
            } else pre = cur;
            cur = cur.next;
        }
        return head;
    }
 public static Node remove(Node head, int val) {
    
    
        Stack<Node> stack = new Stack<Node>();
        while (head != null) {
    
    
            if ((int) head.date != val) {
    
    
                stack.push(head);
            }
            head = head.next;
        }
        while (!stack.empty()) {
    
    
            stack.peek().next = head;
            head = stack.pop();
        }
        return head;
    }

1.1 The variant is to delete duplicate nodes. Instead of deleting all duplicate nodes, keep the first node. If it is repeated later, delete it. The code is also given here:

Insert picture description here

Copyright statement: This article is the original article of the blogger and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting.
Link to this article: http://blog.csdn.net/feinifi/article/details/94460480

2. Print the linked list from beginning to end

  • Solution 1: In typical first-in-last-out, we can use the stack to achieve this sequence. Every time a node is passed, the node is placed in a stack. After traversing the entire linked list, the values ​​of the nodes are output one by one starting from the top of the stack. At this time, the output nodes have been reversed.
  • Solution 2: Use recursive reverse output.
    public static void PrintListReversing(Node head) {
    
    
        Stack<Node> stack = new Stack<Node>();
        while (head != null) {
    
    
            stack.push(head);
            head = head.next;
        }
        while (!stack.isEmpty()) {
    
    
            head = (Node) stack.pop();
            System.out.println(head.date);
        }
    }   
public static void PrintListReversing2(Node head) {
    
    
        if (head != null) {
    
    
            if (head.next != null) {
    
    
                PrintListReversing2(head.next);
            }
        }
        System.out.println(head.date);
    }

Guess you like

Origin blog.csdn.net/Zmd_23/article/details/108718606