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

删除链表中的元素

时间:2017-02-27 12:55:12      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:color   param   head   return   node   val   link   lis   while   

/**
 * 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 res = head;
        if(head==null) return null;
        while(head.next!=null){
            if(head.next.val==val){
                if(head.next.next!=null){
                    head.next = head.next.next;
                }else{
                    head.next=null;
                    break;
                }
            }else{
                head = head.next;
            }
        }
       if(head.val==val) return head.next;
       return res;
    }
   
}

 

删除链表中的元素

标签:color   param   head   return   node   val   link   lis   while   

原文地址:http://www.cnblogs.com/haleny/p/6473132.html

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