算法题第7题-----删除链表中的元素,难度(简单)

题目

删除链表中等于给定值 val 的所有节点。

示例


输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4->5

题解

/**
 * 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) {
        
        //如果传进来得head为空得话,直接返回head
        if(head==null)return head;

        head.next=removeElements(head.next,val);
        return head.val==val?head.next:head;


    }
}

猜你喜欢

转载自blog.csdn.net/qq_38941937/article/details/103085198