【LeetCode】Remove Duplicates from Sorted List
来源:互联网 发布:电气工程师难考吗 知乎 编辑:程序博客网 时间:2024/06/12 00:28
题目描述
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
.
题目分析
去除链表中的重复数字。纯粹的链表操作。
定义两个指针,如果后面那个指针的val和前面的一样,就把后面的删除
代码示例
#include <iostream>#include <vector>using namespace std;/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; void printList(ListNode *head,char *name){printf("%s\n",name);while(head != NULL){printf("%3d,",head->val);head = head->next;}printf("\n");}class Solution {public: ListNode *deleteDuplicates(ListNode *head) { if(head == NULL)return NULL; ListNode *p = head; ListNode *q = p->next; while(q != NULL) { if(p->val == q->val)//把q移除{p->next = q->next;q = p->next; } else{q = q->next;p = p->next;}}return head; }};/*vector 在添加过程中会动态的申请内存,元素的位置会动态的调整,所以不能边创建vector边创建链表,中间指针可能会动。这里的list需要使用传引用 */ListNode* listCreate(vector<ListNode> &list, int arr[], int n){if(n <= 0)return NULL;for(int i=0;i<n;i++){ListNode tmp(arr[i]);list.push_back(tmp);}ListNode *head = &list[0];for(int i=1;i<n;i++){head->next = &list[i];head = head->next;}head = &list[0];return head;}void CheckList(ListNode *head,int arr[], int n){int i = 0;for(i = 0;i<n && head != NULL;i++,head=head->next){if(head->val != arr[i])break;}if(i != n)printf("------------------------failed\n");elseprintf("------------------------passed\n");}//功能测试,长度为偶数 void test0(){vector<ListNode> list;int arr[10] = {0,1,1,3,3,5,6,7,7,7};ListNode *head = listCreate(list,arr, 10);printList(head,"head");Solution so;so.deleteDuplicates(head);int arr1[6] = {0,1,3,5,6,7};CheckList(head,arr1,6);//printList(head,"head");}int main(int argc, char *argv[]){test0(); return 0;}
推荐学习C++的资料
C++标准函数库
http://download.csdn.net/detail/chinasnowwolf/7108919
在线C++API查询
http://www.cplusplus.com/
0 0
- Remove Duplicates from Sorted List :from LeetCode
- Leetcode - List - Remove Duplicates from Sorted List
- [leetcode][list] Remove Duplicates from Sorted List
- [leetcode][list] Remove Duplicates from Sorted List
- LeetCode: Remove Duplicates from Sorted List
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List
- [Leetcode] Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- leetcode 37: Remove Duplicates from Sorted List
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List
- [leetcode] Remove Duplicates from Sorted List
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode]Remove Duplicates from Sorted List
- 第二章 递归与分治策略
- ZOJ 3609 Modular Inverse(模拟)
- 微软今中止撑持XP体系 后XP年代带来多少商机
- Cocos2d-x学习(六):cocos2d-x中的模态对话框简单实现
- openfire 安装 和 遇到的一些问题
- 【LeetCode】Remove Duplicates from Sorted List
- Java SE 6 新特性: JMX 与系统管理
- YII AJAX 原理
- 机器智慧能否超越人类?三派专家观点
- java事务
- opengl
- APK反编译
- Web Service测试工具小汇
- MSSQLSERVER导入EXCEl