LeetCode

     删除链表中的指定元素。

 

/**
 * 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) {
        if(head == null)return null;
        ListNode p,q;
        p = head.next;
        q = head;
        
        while(p!=null) {
            if(p.val == val) {
                q.next = p.next;
                p = p.next;
            }
            else {
                p = p.next;
                q = q.next;
            }
        }
        if(head.val == val) {
            head = head.next;
        }
        
        return head;
    }
}
原文地址:https://www.cnblogs.com/wxisme/p/4458539.html