LintCode(452)

来源:互联网 发布:淘宝助理mac 编辑:程序博客网 时间:2024/05/16 12:05

Remove Linked List Elements

Remove all elements from a linked list of integers that have value val.

样例
Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5

/** * 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) {        // Write your code here        //ListNode p = new ListNode(0);        ListNode dummy = new ListNode(0);        dummy.next = head;        head = dummy;        while(head.next != null){            if(head.next.val == val)                head.next = head.next.next;            else                head = head.next;        }        return dummy.next;    }    }
0 0
原创粉丝点击