Skip to content

Latest commit

 

History

History
50 lines (40 loc) · 1.08 KB

203. Remove Linked List Elements.md

File metadata and controls

50 lines (40 loc) · 1.08 KB

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.

Example:

Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5

Solution

  • 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;
        }