题目描述
删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
总结
SC递归写的漂亮阿
在不能比较好掌握的地方要先脚踏实地,先实现,不要怕写的代码臭
在掌握的地方时候要想着更进一步
Sample Code
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;
}
}
Demo Code
class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head==null) return head;
while(head.val == val) {
if(head.next != null) head = head.next;
else return null;
}
ListNode slow = head;
while(head.next != null) {
if(head.next.val == val)
head.next = head.next.next;
else
head = head.next;
}
return slow;
}
}