My clean 1ms Java code.


  • -1
    M

    My clean 1ms code.
    '''
    public class Solution {
    public ListNode removeElements(ListNode head, int val) {
    if(head==null || (head.next==null && head.val==val))
    return null;
    ListNode cur=head.next,prev=head;
    while(cur!=null){
    if(cur.val==val){
    prev.next=cur.next;
    cur=prev.next;
    }
    else{
    prev=cur;
    cur=cur.next;
    }
    }
    return ((head.val==val)?head.next:head);
    }
    }

    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.