本文是在学习中的总结,欢迎转载但请注明出处: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. Dynamics CRM2016 Web API之创建记录

    前篇介绍了通过primary key来查询记录,那query的知识点里面还有很多需要学习的,这个有待后面挖掘,本篇来简单介绍下用web api的创建记录. 直接上代码,这里的entity的属性我列了几 ...

  2. SQL Server 索引维护(1)——系统常见的索引问题

    前言: 在很多系统中,比如本人目前管理的数据库,索引经常被滥用,甚至使用DTA(数据库引擎优化顾问)来成批创建索引(DTA目前个人认为它的真正用处应该是在发现缺失的统计信息,在以前的项目中,用过一次D ...

  3. Android开发 无法导入ViewPagerIndicator或其他开源框架无法导入

    这个问题又花费了好长时间,其实就是很简单的问题,因为各种开源框架的库名称都叫liberary,如果上次导入其他开源框架没有更改名称的话,你再导入其他第三库的时候,系统发现重名,就提示无法导入现象. 解 ...

  4. iOS中 CoreGraphics快速绘图(详解) 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 第一步:先科普一下基础知识: Core Graphics是基于C的API,可以用于一切绘图操作 Core Graph ...

  5. Android布局性能优化—从源码角度看ViewStub延迟加载技术

    在项目中,难免会遇到这种需求,在程序运行时需要动态根据条件来决定显示哪个View或某个布局,最通常的想法就是把需要动态显示的View都先写在布局中,然后把它们的可见性设为View.GONE,最后在代码 ...

  6. 深入剖析Tomcat会话机制

    1缓存机制 Tomcat默认将Session保存到内存中.但同时,Tomcat也提供了PersistentManager配合不同的Store实现的方式,使Session可以被保存到不同地方(Datab ...

  7. 漫谈android系统(4)bring up panel

    点击打开链接 版权声明: 作者:alex wang 版权:本文版权归作者和CSDN共有 转载:欢迎转载,为了保存作者的创作热情,请按要求[转载],谢谢 要求:未经作者同意,必须保留此段声明:必须在文章 ...

  8. UNIX环境高级编程——线程属性

    pthread_attr_t 的缺省属性值 属性 值 结果 scope PTHREAD_SCOPE_PROCESS 新线程与进程中的其他线程发生竞争. detachstate PTHREAD_CREA ...

  9. (NO.00003)iOS游戏简单的机器人投射游戏成形记(二十一)

    回到Xcode中,在MainScene.h接口中添加碰撞协议: @interface MainScene : CCNode <CCPhysicsCollisionDelegate> //. ...

  10. web安全认证机制知多少

    如今web服务随处可见,成千上万的web程序被部署到公网上供用户访问,有些系统只针对指定用户开放,属于安全级别较高的web应用,他们需要有一种认证机制以保护系统资源的安全,本文将探讨五种常用的认证机制 ...