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

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

思路:

(1)题意为移除已排序链表中重复元素。

(2)首先,对头结点进行判空,为空则返回空。不为空设定first指向head。

(3)其次,判断first的后续节点second是否为空,如果不为空,则比较它们值是否相同。如果值不同,则节点first指向second,

second指向first的后续节点,循环继续;如果值相同,设置节点last指向second的后续节点,如果last不为空,并且last的值

和first的值相同(说明连续三个节点值相同),last指向其后续节点,循环直到ast为空或者first的值和last的值不同,此时,

如果last不为空,则first的后续节点为last,first指向last,second指向first的后续位置,循环继续,如果last为空,说明已经

遍历到最后节点,此first的后续节点指向null,返回head。

(4)其指向过程可以简单如下所示:
         例如:1->1->2->3->3->3->4->5->5->5
                  (A)first=1,second=1,first=second,则last=2,由于first!=last,所以first=2,second=3;
                  (B)first=2,second=3,first!=second,则first=3,second=3;
                  (C)first=3,second=3,first=second,则last=3,由于first=last,循环直到last==null或者first!=last,此时last=4,则first=4,second=5;
                  (D)first=4,second=5,first!=second,则first=5,last=5;
                  (E)first=5,last=5,first=second,last=5,由于first=last,循环直到last==null或者first!=last,此时last=null,则first.next=null,返回head。

算法代码实现如下:

public ListNode deleteDuplicates(ListNode head) {
    if (head == null)
		return null;
	ListNode first = head;
	ListNode second = first.next;
	while (second != null) {
		if (first.val == second.val) {
			ListNode last = second.next;
			while (last != null && first.val == last.val) {
				last = last.next;
			}
			if (last != null) {
				first.next = last;
				first = last;
				second = first.next;
			} else {
				first.next=null;
				return head;
			}

		} else {
			first = second;
			second = first.next;
		}
	}
	return head;
}

Leetcode_83_Remove Duplicates from Sorted List的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  2. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  3. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  5. Leetcode-83 Remove Duplicates from Sorted List

    #83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...

  6. Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  7. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  8. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  9. 26. Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

随机推荐

  1. ubuntu批量更改文件权限

    重装系统之后,把文件从windows分区拷到linux分区发现所有文件的权限全是777,在终端下看到所有文件的颜色都很刺眼,文件有很多,一个一个改不现实,所以写了一段python脚本批量更改文件权限. ...

  2. 函数&语法

    定义一个函数 加上一些算法,由自己定义的函数,以下是简单的规则: 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 (). 任何传入参数和自变量必须放在圆括号中间,圆括号之间可以用于定义参 ...

  3. xshell连接centos与ubuntu

    操作系统:Windows 7 应用软件:Ware Workstation &Xshell 5 Linux:CentOS 7 Minimal &Ubuntu Server 16 ==== ...

  4. Linux常见目录及命令介绍

    一.Linux中常用的目录介绍:     /        -根目录     /bin    -命令保存目录(普通用户亦可读取的命令)     /boot    -启动目录,启动相关文件     /d ...

  5. Git 直接推送到生产服务器

    假设路径为/project/path/ 设定git仓库可以直接被远程推送(需要较新的git版本,比如2.7) cd /project/path git config receive.denyCurre ...

  6. 周口网视界易付点卡销售平台招商中 www.zkpay.cn 欢迎各界朋友加盟合作。

    周口网视界易付点卡销售平台针对全国各地网吧及游戏点卡代理招商中. http://www.zkpay.cn   腾讯新的游戏点卡销售平台,平台价优稳定,这个是老家朋友开的公司,欢迎全国各地网吧客户及游戏 ...

  7. 计算机网络之文件传送协议FTP

    FTP 文件传送协议FTP(File Transfer Protocol)是因特网上使用最广泛的文件传送协议. FTP 提供交互式的访问,允许客户指明文件的类型与格式,并允许文件具有存取权限.FTP ...

  8. log file sync 等侍值高的一般通用解决办法

    log file sync等待时间发生在redo log从log buffer写入到log file期间. 下面对log file sync做个详细的解释. 何时发生日志写入: 1.commit或者r ...

  9. Thread Pools

    许多程序会动态创建数十个设置上百个线程.举个例子,一个web服务器可能在每一个新到来的请求时创建一个新线程,然后在请求完成后将其终止. 然而,创建一个新线程将会带来一定的耗费:它需要在内核中创建自身必 ...

  10. J2EE进阶(十七)Hibernate中常用的HQL查询方法(getHibernateTemplate())

    J2EE进阶(十七)Hibernate中常用的HQL查询方法(getHibernateTemplate())   当我们使用Hibernate进行数据的CRUD操作时,利用模版进行操作不失为一种方法. ...