1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummy = new ListNode(-1); dummy.next = head; ListNode previous = dummy; while (head != null){ ListNode next = head.next; if (head.val == val){ previous.next = head.next; head.next = null; }else{ previous = head; } head = next; } return dummy.next; } } |
No comments:
Post a Comment