[LeetCode] 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.
题目解析:
这是一道非常简单的链表题目,题意是删除单链表(已排序)中的重复数字,只需一次判断前后两个结点数字是否相等即可。需要注意几点:
1.该链表中头结点就开始存储数字head->val存在;
2.初始判断if(head==NULL || head->next==NULL),防止出现'[]'或'[1]';
3.判断过程中使用q->next->val==q->val和释放free临时结点p;
4.若使用中间变量number=q->val判断时,当else中q指向下一个结点为空时,该句不存在number=q->val会报错RE,如'[1,1]'。故不建议使用临时变量。
总之,一道非常基础的链表题目,不需要过于复杂化代码。
我的代码:
/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/
struct ListNode* deleteDuplicates(struct ListNode* head) {struct ListNode *p,*q;q=head;if(head==NULL || head->next==NULL) //防止[]和[1]return head;while(q) {if( q->next!=NULL && q->next->val==q->val ) {//删除操作 最后freep=q->next;q->next=p->next;free(p);}else {q=q->next;}}return head;
}
其他类型链表题目:
(By:Eastmount 2015-9-10 凌晨3点半 http://blog.csdn.net/eastmount/ )
[LeetCode] Remove Duplicates from Sorted List - 链表问题相关推荐
- LeetCode:Remove Duplicates from Sorted List I II
LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
- [LeetCode] Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
- 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
题意:给出一个单链表 ,将其中重复的元素删除 思路:在找重复结点时,需要找到其前继结点 代码如下: class Solution {public ListNode deleteDuplicates(L ...
- LeetCode() Remove duplicates from sorted list II
ListNode* dummy = new ListNode(0); //必须要加上 new ListNode(0); 否则有错误. dummy->next = head; head = dum ...
- leetcode(83)—— 删除已排序链表重复元素(Remove Duplicates from Sorted List)
Remove Duplicates from Sorted List 官方答案(Java): Remove Duplicates from Sorted List 思路:维护两指针(ListNode ...
- Remove Duplicates from Sorted Array II -- LeetCode
原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 这道题跟Remove Duplicates ...
- LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array
LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array 问题 Given a sorted array nums, remove the du ...
最新文章
- 国外设计师眼中的原型工具Mockplus
- C语言函数strstr()分析及实现
- 用Windows API实现多线程--原理例子
- XCTF easyCpp buu [MRCTF2020]EasyCpp
- mysql model only_full_group_by_MySql版本问题sql_mode=only_full_group_by的完美解决方案
- Android ExpandableListView几个特殊的属性
- 【51Nod-1100】 斜率最大(贪心)☆双排序
- Nodejs cluster模块深入探究
- 什么叫做java程序中的继承_【Java】基础16:什么叫继承?
- JS中定时器的返回数值ID值
- 迈信EP100伺服驱动器方案
- 服务器pe系统u盘启动不了,U盘安装系统进不了PE怎么办?U盘装系统进不去pe如何解决?...
- PostgreSql增删改(与mysql差异)
- USACO-Section3.2 Feed Ratios【克莱默法则】
- 代数数、超越数、代数函数、超越函数
- 【重要公告】包头市新型冠状病毒感染肺炎防控工作指挥部公告(2022年第4、5、6、7号)
- Java---设计【超市商品管理系统】
- Win7安全模式启动卡在Classpnp.sys
- can收发器 rx_Microchip工程师社区 - 两组PIC18F25K80+CAN收发器的CAN通讯 - 16位MCU及DSC - 麦田论坛...
- 吉米_王:乌班图下安装pycharm的方式
热门文章
- seconds_behind_master监控复制推延的不足及pt-heartbeat改进方法
- oracle查询排序asc/desc 多列 order by
- ORACLE TEXT LEXER PREFERENCE(一)
- Spring factoryBeanInstanceCache在哪里进行put?
- MySQL配置(二)
- cookie的设置和获取
- android6.0的坑
- C# 强制关闭当前程序进程(完全Kill掉不留痕迹)
- hdu 1047 Integer Inquiry
- 分享codeigniter框架,在zend studio 环境下的代码提示