leetcode83 Remove Duplicates from Sorted List
题意:删掉单链表里重复的节点,如:
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
思路:真的真的是很简单的题啊,但是有个陷阱,也怪自己练的太少,都忘记了这个。思路其实就是判断如果p->val == p->next->val就删掉p->next节点,如果新的p->next的值与p的值不相等则往后移动。这里的判断条件有两个,
一个是p->val != p->next->val,一个是p->next != NULL。
代码:
ListNode* deleteDuplicates(ListNode* head)
{
ListNode* p = head;
if(head == NULL || head->next == NULL)
return head;
while(p->next != NULL)
{
if(p->val == p->next->val)
{
ListNode* temp = p->next;
p->next = p->next->next;
free(temp);
}
if(p->next != NULL && p->val != p->next->val)
{
p = p->next;
}
}
return head;
}
leetcode83 Remove Duplicates from Sorted List的更多相关文章
- Leetcode-83 Remove Duplicates from Sorted List
#83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
随机推荐
- [Leetcode] search a 2d matrix 搜索二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- Win7命令mklink的使用
C盘空间越来越小,在Win7里还标红了,心里看得不舒服,得想一些方法腾出一些空间.看了AppData,Chrome占了1G多的空间. 当时安装Chrome浏览器时因为不能指定安装目录,所以Chrome ...
- [CF816E] Karen and Supermarket1 [树形dp]
传送门 - > \(CF816E\) Karen and Supermarket 题意翻译 在回家的路上,凯伦决定到超市停下来买一些杂货. 她需要买很多东西,但因为她是学生,所以她的预算仍然很有 ...
- HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- c++(类继承)示例[仅用于弱弱的博主巩固知识点用哦,不好勿喷]
测试代码: Animals.h: #pragma once #include<string> class Animals { protected: std::string Food; in ...
- CSS3学习之radial-gradient(径向渐变)
转自:http://www.cnblogs.com/rainman/p/5133685.html 1.语法 径向渐变不同于线性渐变,线性渐变是从“一个方向”向“另一个方向”的颜色渐变,而径向渐变是从“ ...
- ES6学习笔记(一)——Promise
Promise 是 ES6 提供的一种异步编程的解决方案: 将异步操作以同步操作的流程表达出来,避免了层层嵌套的回调函数(解决异步函数回调地狱的问题).Promise 对象保存着异步操作的结果. 首先 ...
- 复选框 checkbox 选中事件
项目中用的jquery-1.11 今天需要检测一个checkbox的选中状态,想当然的用 .attr("checked") ,结果发现,无论是否选中,这个值都是 undefined ...
- Ubuntu pppoe 拨号上网
-------------蓝色是终端里面的连接方式,可以不看--------------------- ADSL上网,Ubuntu下是可以的,虽然以前没用过拨号上网,不过查了查也不是很麻烦. 打开终端 ...
- c#之字符串函数
1.常用的字符串函数 Compare 比较字符串的内容,考虑文化背景(场所),确定某些字符是否相等 int Compare(string str1,string str2) int Compare(s ...