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


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)该题主要考察链表的操作。首先,对于类似a->a->a->b->c....这样需要移除a的链表进行判断,得到第一个和目标值不相同的节点,设定指针r指向该节点,这个节点即为结果链表的起始节点;其次,设定指针s和t分别指向当前节点和当前节点的下一个节点,其中s用来标记待删除节点后面的节点,t用来标记待判断需删除的节点。这样,对链表进行遍历,如果t指向节点的值和目标值相同,则t指向其所指节点的下一个节点,s的下一个节点指向t;如果t指向节点的值和目标值不相同,则s和t分别后移;最后,遍历完整个链表,所得r即为结果链表。

(3)详情见下方代码。希望本文对你有所帮助。

算法代码实现:

/**
	 * @author liqqc
	 * @param head
	 * @param val
	 */
	public static ListNode removeElements(ListNode head, int val) {
		if (head == null)
			return null;

		ListNode r = head;
		ListNode s = head;
		ListNode t = head.next;
		// 起始位置的确定
		while (s != null && s.val == val) {
			s = s.next;
			if (t != null) {
				t = t.next;
			}
			r = s;
		}

		while (t != null) {
			// 如果相同则移除
			if (t.val == val) {
				t = t.next;
				s.next = t;
			} else {
				s = s.next;
				t = t.next;
			}
		}
		return r;
	}

Leetcode_203_Remove Linked List Elements的更多相关文章

  1. [LintCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Have you met this question i ...

  2. Leetcode-203 Remove Linked List Elements

    #203.   Remove Linked List Elements Remove all elements from a linked list of integers that have val ...

  3. 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements

    237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...

  4. leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)

    203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...

  5. 【LeetCode】203. Remove Linked List Elements

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

  6. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  7. LeetCode Remove Linked List Elements 删除链表元素

    题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...

  8. LeetCode_203. Remove Linked List Elements

    203. Remove Linked List Elements Easy Remove all elements from a linked list of integers that have v ...

  9. LeetCode--LinkedList--203. Remove Linked List Elements(Easy)

    203. Remove Linked List Elements(Easy) 题目地址https://leetcode.com/problems/remove-linked-list-elements ...

随机推荐

  1. Java 资源本地化与国际化

    资源包 在编写应用程序的时候,需要面对的一个问题是如何来处理与locale相关的一些信息.比如,页面上的一些静态文本就希望能够以用户习惯的语言显示.最原始的做法是将这些信息硬编码到程序中(可能是一大串 ...

  2. Pycharm中进行Python远程开发

    http://blog.csdn.net/pipisorry/article/details/52269952 PyCharm提供两种远程调试(Remote Debugging)的方式:    配置远 ...

  3. CVS简介

    CVS - Concurrent Versions System(并发版本管理系统)是一个版本控制管理系统,它是SVN出现之前最为广泛使用的一个版本控制系统. CVS的优点就不多说了,总之没有它,早期 ...

  4. Universal-Image-Loader 图片异步加载类库的使用

    这个图片异步加载并缓存的类已经被很多开发者所使用,是最常用的几个开源库之一,主流的应用,随便反编译几个火的项目,都可以见到它的身影. 可是有的人并不知道如何去使用这库如何进行配置,网上查到的信息对于刚 ...

  5. FORM执行查询的各种方法

     一.FORM调用FORM后执行查询 1.打开 APPSTAND.fmb,把 Object Groups 下的 QUERY_FIND 对象组拖动到自己的 form 中的 Object Groups ...

  6. tf.app.run()

    在很多TensorFlow公布的Demo中,都有这样的代码存在,如下,这是干什么的呢? if __name__ == "__main__": tf.app.run() 我们来看一下 ...

  7. cocos2d-x 3.11 游戏开发环境搭建流程

    cocos2d-x 3.11.1 游戏开发环境搭建流程 1. 准备下面的软件 1) Windows7 64Bit+ VS2013 (VC++) 这个不用多说. 2) cocos2d-x-3.11.1. ...

  8. FFmpeg源代码简单分析:常见结构体的初始化和销毁(AVFormatContext,AVFrame等)

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  9. javascript之页面打印

    WebBrowser组件是IE内置的浏览器控件,使用时,首先要在<body>标签的下面用<object>...</object>标记声明WebBrowser组件,代 ...

  10. UNIX网络编程——Socket通信原理和实践

    我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览器浏览网页时,浏览器的进程怎么与web服务器通信的?当你用QQ聊天时,QQ进程怎么与服务器或你好友所在的QQ进程通信?这些都得靠so ...