码迷,mamicode.com
首页 > 其他好文 > 详细

[leedcode]Remove Linked List Elements

时间:2015-04-30 23:04:59      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

/**
 * 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;
        
    }
}

 

[leedcode]Remove Linked List Elements

标签:

原文地址:http://www.cnblogs.com/qiaomu/p/4469968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!