Home > Java > Javagetting Started > body text

Java implements the operation of removing elements from a linked list

王林
Release: 2021-01-04 11:54:23
forward
1691 people have browsed it

Java implements the operation of removing elements from a linked list

Purpose: Remove linked list elements

(Learning video sharing: java teaching video)

Problem introduction:

Delete all nodes in the linked list that are equal to the given value * "val*".

Example description:

示例:
 
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
Copy after login

Problem solution ideas:

One is based on the sentinel node method, and the other is based on the java collection method. The essence is still the same.

Program implementation:

 import java.util.*;
 
public class RemoveElementsTest3 {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(6);
        ListNode l4 = new ListNode(3);
        ListNode l5 = new ListNode(4);
        ListNode l6 = new ListNode(5);
        ListNode l7 = new ListNode(6);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = l5;
        l5.next = l6;
        l6.next = l7;
        ListNode listNode = removeElements2(l1, 6);
        System.out.println("listNode = " + listNode);
 
    }
 
    public static ListNode removeElements(ListNode head, int val) {
        ListNode dummyNode = new ListNode(0);
        ListNode currentNode = dummyNode;
        while (head != null) {
            if (head.val != val) {
                currentNode.next = head;
                currentNode = currentNode.next;
            }
            head = head.next;
        }
        currentNode.next = null;
        return dummyNode.next;
    }
 
    public static ListNode removeElements2(ListNode head, int val) {
        List<Integer> list = new LinkedList<>();
        while (head != null) {
            list.add(head.val);
            head = head.next;
        }
        List<Integer> tempList = new ArrayList<>();
        tempList.add(val);
        list.removeAll(tempList);
        ListNode dummyNode = new ListNode(0);
        ListNode tempNode = dummyNode;
        for (int i = 0, size = list.size(); i < size; i++) {
            ListNode listNode = new ListNode(list.get(i));
            tempNode.next = listNode;
            tempNode = tempNode.next;
        }
        return dummyNode.next;
    }
}
Copy after login

Summary:

Actually, after writing this, I think it is easy to understand if you look at the implementation logic of the entire program. There is nothing wrong with it. The difficult points need to be deduced. If you don't understand, you can debug the given program multiple times. The content output each time is output only after it is successfully run.

Related recommendations: java introductory tutorial

The above is the detailed content of Java implements the operation of removing elements from a linked list. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!