题解 | 移除链表元素
移除链表元素
https://www.nowcoder.com/practice/428a854dff8b4333b54cfe580323e2df
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
public ListNode removeElements (ListNode head, int val) {
// write code here
if(head==null)return null;
while(head.val==val&&head!=null){
head = head.next;
}
ListNode res = head;
while(head!=null&&head.next!=null){
ListNode temp1 = head.next;
if(temp1.val==val){
head.next = head.next.next;
}
else{
head = head.next;
}
}
return res;
}
}
文远知行公司福利 589人发布