Leetcode
2020.05.04 08:54
203. Remove Linked List Elements
조회 수 731 추천 수 0 댓글 0
Remove all elements from a linked list of integers that have value val.
Example:
Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode removeElements(ListNode head, int val) { ListNode curr = head; ListNode prev = new ListNode(-1); prev.next = head; head = prev; while(curr != null){ if(curr.val == val){ prev.next = curr.next; }else{ prev = prev.next; } curr = curr.next; } return head.next; } }
[문제] https://leetcode.com/problems/remove-linked-list-elements/