[leedcode]Remove Linked List Elements

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
  public ListNode removeElements(ListNode head, int val) {
        ListNode p=head;
        if(head==null) return head;
        while(p!=null&&p.val==val) {
            head=head.next;
            p=head;
        }
        while(p!=null&&p.next!=null){
            if(p.next.val==val)
                p.next=p.next.next;
            else
                p=p.next;
        }
        return head;
        
    }
}

 

posted @ 2015-04-30 21:45  ~每天进步一点点~  阅读(97)  评论(0编辑  收藏  举报