LeetCode Remove Linked List Elements

原创 2015年07月09日 00:40:08

Description:

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

Solution:

需要单独考虑链表头。

import java.util.*;

public class Solution {
	public ListNode removeElements(ListNode head, int val) {
		while (head != null && head.val == val) {
			head = head.next;
		}

		if (head == null)
			return null;

		ListNode temp = head;
		while (temp.next != null) {
			if (temp.next.val == val) {
				temp.next = temp.next.next;
			} else {
				temp = temp.next;
			}
		}

		return head;
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode 203. Remove Linked List Elements

Remove all elements from a linked list of integers that have value val.Example Given: 1 –> 2 –> 6 –...

Leetcode_203_Remove Linked List Elements

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/45868027 Remove all el...

leetcode 203. Remove Linked List Elements

我觉得在使用链表的时候一般有两种思路:一种是链表的内容可以修改,在原链表上进行处理,后续的操作不需要使用原链表的内容的时候这种思路可以节约空间和时间;另外一种是当我们不能更改链表的内容,后续需要用到的...

LeetCode Remove Linked List Elements

题目: Remove all elements from a linked list of integers that have value val. Example Given: ...

<LeetCode OJ> 203. Remove Linked List Elements

203. Remove Linked List Elements My Submissions Question Total Accepted: 45239 Total Submissions...

关于LeetCode中Remove Linked List Elements一题的理解

题目如下: Remove all elements from a linked list of integers that have value val. Example Given: 1 --...

leetcode题解-203. Remove Linked List Elements

题目:Remove all elements from a linked list of integers that have value val.Example Given: 1 --> 2 -->...

[LeetCode]203 Remove Linked List Elements

原题链接QuestionRemove all elements from a linked list of integers that have value val.Example Given: 1...

[LeetCode] Remove Linked List Elements

Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 –> 6...

LeetCode 203 Remove Linked List Elements(移除链表元素)(Linked List)(*)

翻译从一个链表中移除所有值为val的元素。例如 给定:1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 返回:1 --> 2 --> 3 --> 4 -->...
  • NoMasp
  • NoMasp
  • 2016-01-28 08:58
  • 1723
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)