[LeetCode] 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
这道题两个指针就可以了哇。不过我总是把指针搞混。所以我还是觉得有点绕T T
public class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode track = new ListNode(0);
track.next = head;
ListNode a = track;
ListNode b = head;
while(b!=null) {
if(b.val == val) {
a.next = b.next;
} else {
a = a.next;
}
b = b.next;
}
return track.next;
}
}
浙公网安备 33010602011771号