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

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

 

Hide Tags
 Linked List 

链接:  http://leetcode.com/problems/remove-linked-list-elements/

2/23/2017, Java

 1 public class Solution {
 2     public ListNode removeElements(ListNode head, int val) {
 3         ListNode dummy = new ListNode(0);
 4         dummy.next = head;
 5         ListNode current = dummy;
 6 
 7         while (current.next != null) {
 8             if (current.next.val == val) {
 9                 current.next = current.next.next;
10             } else {
11                 current = current.next;
12             }
13         }
14 
15         return dummy.next;        
16     }
17 }

 

posted @ 2017-02-24 04:35  panini  阅读(95)  评论(0编辑  收藏  举报