Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Remove all elements from a linked list of integers that have value val.
- * Definition for singly-linked list.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int x) { val = x; }
- * }
- */
- class Solution {
- public ListNode removeElements(ListNode head, int val) {
- ListNode start = new ListNode(0); //create new node
- start.next = head; //point new node to the original head
- ListNode i = start; //create an iterator node and set it to also point to the head
- while(i.next != null){ //while there is another node following
- if(i.next.val == val){ //if the val to find is equal to the NEXT node's value
- ListNode temp = i.next; //set the node to be removed into the holder node
- i.next = temp.next; //move the holder's NEXT node into the iterator node
- }else{
- i = i.next;
- }
- }
- return start.next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement