"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)"