解题思路:
1.该函数返回的是一个链表
2.递归终止条件,当结点为null时返回null
3.寻找“等价”关系式,最终链表 == (跳过val的)多个链表相加
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val){
if(head == null){
return null;
}
if(head.val == val){
head = removeElements(head.next,val);//如果结点的数据==val,那么跳过该节点
return head;
}else{
head.next = removeElements(head.next,val);//如果不是,接下一节点即可
return head;
}
}
}