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
https://leetcode.com/problems/remove-linked-list-elements/
链表。
1 /** 2 * @param {ListNode} head 3 * @param {number} val 4 * @return {ListNode} 5 */ 6 var removeElements = function(head, val) { 7 var res = new ListNode(-1); 8 res.next = head; 9 var current = head; 10 var previous = res; 11 while(current !== null){ 12 if(current.val === val){ 13 previous.next = current.next; 14 }else{ 15 previous = current; 16 } 17 current = current.next; 18 } 19 20 return res.next; 21 };
时间: 2024-10-06 08:56:23