leetcode Daily Challenge on July 20th, 2020.
Difficulty : Easy
Related Topics : LinkedList
Remove all elements from a linked list of integers that have value val.
Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5
- mine
- Java
Runtime: 1 ms, faster than 90.05%, Memory Usage: 40.6 MB, less than 32.91% of Java online submissions
// O(N)time // O(1)space public ListNode removeElements(ListNode head, int val) { ListNode res = new ListNode(); res.next = head; ListNode t = res; while(t.next != null){ if(t.next.val == val){ while(t.next != null && t.next.val == val){ t.next = t.next.next; } }else{ t = t.next; } } return res.next; }
- Java