0%

Leetcode 203 Remove Linked List Elements

Remove all elements from a linked list of integers that have value val\.

Example:

1
2
Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5

模拟

直接做即可, 注意添上头节点更简单. 还要注意两个val相邻的情况. 如果进行了删除操作i.next = i.next.next,就不要i = i.next了. 否则会错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode dummyHead = new ListNode(0, head);
ListNode i = dummyHead;
while(i != null)
{
if(i.next == null)
break;
if(i.next.val == val)
{
i.next = i.next.next;
}
else
{
i = i.next;
}
}
return dummyHead.next;
}
}