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
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 public class Solution { 10 public ListNode removeElements(ListNode head, int val) { 11 ListNode header = new ListNode(-1); 12 header.next = head; 13 ListNode pre = header; 14 ListNode cur = head; 15 while(cur != null){ 16 if(cur.val == val){ 17 pre.next = cur.next; 18 }else{ 19 pre = pre.next; 20 } 21 cur = cur.next; 22 } 23 return header.next; 24 } 25 }
posted on 2015-04-29 10:05 Step-BY-Step 阅读(133) 评论(0) 编辑 收藏 举报