链表天然的递归性

删除所有值为v的元素
递归函数的“宏观”语意
在以head为头节点的链表中删除值为val的节点,并返回结果链表的头结点
可不可以不返回头节点
不可以,否则递归函数的结果不能和前面的链表连接起来
public class Simple203 {static class Solution {public ListNode removeElements(ListNode head, int val) {if (head == null) {return null;}head.next = removeElements(head.next, val);return head.val == val ? head.next : head;}}public static void main(String[] args) {int[] nums = {1, 2, 6, 3, 6, 5, 7, 5};ListNode head = new ListNode(nums);System.out.println(head);ListNode listNode = new Solution().removeElements(head, 6);System.out.println(listNode);}}

