您的位置:首页 > 其它

lintcode 删除链表中的元素

2018-01-22 15:50 225 查看
/**

 * Definition for singly-linked list.

 * public class ListNode {

 *     int val;

 *     ListNode next;

 *     ListNode(int x) { val = x; }

 * }

 */

public class Solution {

    /*

     * @param head: a ListNode

     * @param val: An integer

     * @return: a ListNode

     */

    public ListNode removeElements(ListNode head, int val) {

        ListNode first=head;

        if(first==null){

            return null;

        }

        while(first.val==val){

            first=first.next;

            if(first==null){

            

                return null;

            }

        }

        ListNode a=first;

        ListNode b=a.next;

        while(b!=null){

            if(b.val==val){

                a.next=b.next;

               

            }else{

                a=a.next;

            }

             b=b.next;

        }

        return first;

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: