-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
通过次数105,110提交次数226,629
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/remove-linked-list-elements
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码1 (java) 2020-09-30 -_-
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
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; //如果节点相等选择下一个节点
}
}
Reactions are currently unavailable