Java [Leetcode 83]Remove Duplicates from Sorted List
题目描述:
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.
代码如下:
代码一,正常解法:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null)
return null;
ListNode cur = head.next;
ListNode pre = head;
while(cur != null){
if(cur.val == pre.val){
if(cur.next != null){
cur.val = cur.next.val;
cur.next = cur.next.next;
}else{
pre.next = null;
cur = null;
}
} else {
pre = cur;
cur = cur.next;
}
}
return head;
}
}
思路二,递归解法:
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null)return head;
head.next = deleteDuplicates(head.next);
return head.val == head.next.val ? head.next : head;
}
Java [Leetcode 83]Remove Duplicates from Sorted List的更多相关文章
- [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
- LeetCode 83. Remove Duplicates from Sorted List (从有序链表中去除重复项)
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- Java [leetcode 26]Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- [LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序链表中删除重复项)
描述 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...
- [leetcode]83. Remove Duplicates from Sorted List有序链表去重
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
- [LeetCode] 83. Remove Duplicates from Sorted List_Easy tag: Linked List
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
- [LeetCode]83. Remove Duplicates from Sorted List(排序链表去重)
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- leetCode 83.Remove Duplicates from Sorted List(删除排序链表的反复) 解题思路和方法
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- Leetcode 83 Remove Duplicates from Sorted List 链表
就是将链表中的重复元素去除 我的方法很简单就是如果链表的前后元素相同的话,将后一个元素删除 /** * Definition for singly-linked list. * struct List ...
随机推荐
- 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "stdafx.h ...
- Linux/Ubuntu常用快捷键
问题描述: Linux/Ubuntu常用快捷键 问题解决: +++++++++++++++++++ 全局系统 +++++++++++++++++++++ Alt + F1:相当于w ...
- 【POJ】【3308】Paratroopers
网络流/二分图最小点权覆盖 sigh……这题……TLE&RE了好几发 建一个二分图,左边的每个结点代表行,右边的代表列,如果在(i,j)这个位置有一个外星人,那么我们就连一条边 (左 i -& ...
- POJ 1577 Falling Leaves (子母二叉树,给出叶子节点的删除序列,求前序遍历)
题意:给出一棵字母二叉树删除叶子节点的序列,按删除的顺序排列.让你输出该棵二叉树额前序遍历的序列.思路:先把一棵树的所有删除的叶子节点序列存储下来,然后从最后一行字符串开始建树即可,最后遍历输出. ...
- .net中的认证(authentication)与授权(authorization)
“认证”与“授权”是几乎所有系统中都会涉及的概念,通俗点讲: 1.认证(authentication) 就是 "判断用户有没有登录?",好比windows系统,没登录就无法使用(不 ...
- SNAT
http://blog.chinaunix.net/uid-2628744-id-2454879.html
- [转]C++常见内存错误汇总
在系统开发过程中出现的bug相对而言是比较好解决的,花费在这个上面的调试代价不是很大,但是在系统集成后的bug往往是难以定位的bug(最好方式是打桩,通过打桩可以初步锁定出错的位置,如:进入函数前打印 ...
- java retention注解
Retention注解 Retention(保留)注解说明,这种类型的注解会被保留到那个阶段. 有三个值:1.RetentionPolicy.SOURCE —— 这种类型的Annotations只在源 ...
- 删除元素 不存在 NO 存在 输出余下元素
#include<stdio.h> #include<stdlib.h> #define N 5 #define NULL 0 #define OK 1 #define ERR ...
- 示例:Servlet显示当前系统时间(时间格式化)
package com.mhb; import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDate ...