203. 移除链表元素
小于 1 分钟
203. 移除链表元素简单
class Solution {
public ListNode removeElements(ListNode head, int val) {
if (head == null) return null;
ListNode res = new ListNode();
res.next = head;
ListNode node = res;
while (node.next != null){
if(node.next.val == val){
if (node.next.next != null) node.next = node.next.next;
else node.next = null;
}else {
node = node.next;
}
}
return res.next;
}
}
Powered by Waline v2.15.5