对于上次这个问题
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
这次我们用递归
public static ListNode removeElements(ListNode head, int val) { if(head==null) return null; if(head.val==val){ return removeElements(head.next,val); }else{ head.next =removeElements(head.next,val); return head ; } }
图解: