[LeetCode]Remove Linked List Elements

public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        head = dummy;
        ListNode pre = dummy;
        ListNode p = pre.next;
        while (p != null) {
            if (p.val != val) {
                pre = p;
                p = p.next;
            } else {
                pre.next = pre.next.next;
                p = pre.next;
            }
        }
        return head.next;
    }
}

 

posted @ 2015-12-01 05:12  Weizheng_Love_Coding  阅读(129)  评论(0编辑  收藏  举报