203. Remove Linked List Elements
程序员文章站
2024-02-19 08:18:46
...
题目203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
public class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode tempHead = new ListNode(1);
tempHead.next = head;
ListNode preNode = tempHead;
ListNode node = preNode.next;
while(node != null){
if(node.val == val){
preNode.next = node.next;
}else{
preNode = node;
}
node = preNode.next;
}
return tempHead.next;
}
}
上一篇: js获取url中get后面的参数值
推荐阅读
-
203. Remove Linked List Elements
-
LeetCode(52)-Remove Linked List Elements
-
203. Remove Linked List Elements
-
Linked List - Insert, Remove
-
Remove Linked List Elements
-
Remove Linked List Elements
-
Remove Linked List Elements
-
Remove Linked List Elements
-
LeetCode(52)-Remove Linked List Elements
-
203. Remove Linked List Elements