classSolution{ public ListNode removeElements(ListNode head, int val){ ListNode re = new ListNode(0,head); ListNode h = re; while(re.next!=null){ if(re.next.val==val){ re.next = re.next.next; }else{ re = re.next; }
}
return h.next; } }
时间复杂度
空间复杂度
O(n)
O(1)
递归
1 2 3 4 5 6 7
classSolution{ public ListNode removeElements(ListNode head, int val){ if(head==null)return head; head.next = removeElements(head.next, val); return head.val==val?head.next:head; } }