"Leetcode-Interview algorithm classic-java Implementation" "203-remove Linked list Elements (delete elements from a single linked list)"

Source: Internet
Author: User
Tags prev

"203-remove Linked list Elements (remove elements from single-linked list)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" code Download "Https://github.com/Wang-Jun-Chao" Original Question

Remove all elements from a linked list of integers, that has value Val.
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6 , val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5

Main Topic

Given a value of Val, delete the node with the value Val in the single-linked list.

Thinking of solving problems

Adds a node to the list header, iterates through the linked list, and deletes the operation.

Code Implementation

Linked list Node class

publicclass ListNode {    int val;    ListNode next;    ListNode(int x) { val = x; }}

Algorithm implementation class

 Public classSolution { PublicListNoderemoveelements(ListNode Head,intval) {ListNode root =NewListNode (1); Root.next = head;//To record the precursor node of the element to be processedListNode prev = root;//Prev.next indicates the node to be processed         while(Prev.next! =NULL) {//The node to be processed is the node to be deleted            if(Prev.next.val = = val) {//Delete a nodePrev.next = Prev.next.next; }//The node currently being processed does not need to be deleted, Prev moves to the next junction.            Else{prev = Prev.next; }        }//Return to new root node        returnRoot.next; }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47997657"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "203-remove Linked list Elements (delete elements from a single linked list)"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.