首页 > 其他 > 详细

删除链表中的元素

时间:2017-02-27 12:55:12      阅读:195      评论:0      收藏:0      [点我收藏+]
/**
 * 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;
    }
   
}

 

删除链表中的元素

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!