C实现 LeetCode->Remove Duplicates from Sorted List (枚举法)(单链表去重复节点)
来源:互联网 发布:java 线程等待和唤醒 编辑:程序博客网 时间:2024/05/19 00:13
/**
* 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: 删除所有的重复值节点(保证单链表中的值是不唯一的)
*/
//// RemoveDuplicatesfromSortedList.c// Algorithms//// Created by TTc on 15/6/22.// Copyright (c) 2015年 TTc. All rights reserved.///** * 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: 删除所有的重复值节点(保证单链表中的值 是不唯一的) */#include "RemoveDuplicatesfromSortedList.h"#include <stdlib.h>#include <string.h>#include "List.h"/********************************************************************/// LeetCode 答案/********************************************************************/struct ListNode { int val; struct ListNode *next;};//单链表已经是排序好的struct ListNode*deleteDuplicates(struct ListNode* head) { struct ListNode *curr = head; while(curr != NULL && curr->next != NULL){ //如果当前节点的值 等于 当前节点的下一个节点的值 则删除 当前节点指向的下一个节点 if(curr->next->val == curr->val) curr->next = curr->next->next; else curr = curr->next; //否则让当前节点指向下一个元素节点;准备下一次遍历条件 } return head;}/********************************************************************//********************************************************************//********************************************************************/// List.c 是范性类 单链表/********************************************************************/ListElmt*tt_deleteDuplicates(ListElmt* head) { ListElmt *curr = head; while(curr != NULL && curr->next != NULL){ //如果当前节点的值 等于 当前节点的下一个节点的值 则删除 当前节点指向的下一个节点 if((*(int *)(curr->next->data)) == (*(int *)(curr->data))) curr->next = curr->next->next; else curr = curr->next; //否则让当前节点指向下一个元素节点;准备下一次遍历条件 } return head;}voidtest_tt_deleteDuplicates(){ List l1; list_init(&l1, free); int *data ; int array[15] = {1,1,2,3,3,4,5,5,5,6}; //输入时排序好的链表 for (int i = 0; i< 10; i++) { if ((data = (int *)malloc(sizeof(int))) == NULL) return ; *data = array[i]; if (list_ins_next(&l1, NULL, data) != 0) //逐个插入元素 return; } print_list(&l1); ListElmt *result = tt_deleteDuplicates(list_head(&l1)); printf("result->val===%d\n",*(int *)result->data); print_listNode(result);}
0 0
- C实现 LeetCode->Remove Duplicates from Sorted List (枚举法)(单链表去重复节点)
- C实现 LeetCode->Remove Duplicates from Sorted List II(枚举法)(单链表)
- [leetcode]Remove Duplicates from Sorted List (删除有序节点的值重复的节点 C语言)
- LeetCode 82. Remove Duplicates from Sorted List II(删除链表中的重复节点)
- LeetCode 83. Remove Duplicates from Sorted List(删除重复节点)
- [leetcode](Remove Duplicates from Sorted List II C语言实现)
- [leetcode-83]Remove Duplicates from Sorted List(C)
- Leetcode:Remove Duplicates from Sorted List 删除单链表中重复的节点
- *(leetcode) Remove Duplicates from Sorted List
- (LeetCode)Remove Duplicates from Sorted List --- 链表中删除重复元素
- leetcode系列(66)Remove Duplicates from Sorted List,Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List :from LeetCode
- 【LeetCode-面试算法经典-Java实现】【082-Remove Duplicates from Sorted List II(排序链表中删除重复元素II)】
- 【LeetCode-面试算法经典-Java实现】【083-Remove Duplicates from Sorted List(排序的单链表中删除重复的结点)】
- Leetcode - List - Remove Duplicates from Sorted List
- [leetcode][list] Remove Duplicates from Sorted List
- [leetcode][list] Remove Duplicates from Sorted List
- [C++]LeetCode: 5 Remove Duplicates from Sorted List
- Merge Sorted Array算法详解
- NOJ1057 封杀病毒(pow & sqrt函数的原型)
- java学习之旅05--Helloworld深化,文本编辑器的使用,注释
- java、groovy中文转码
- ASP.NET MVC4中用 BundleCollection使用问题手记
- C实现 LeetCode->Remove Duplicates from Sorted List (枚举法)(单链表去重复节点)
- grails转换base64
- iOS 数据持久化三-NSUserDefaults
- groovy构建和解析 XML
- AC自动机
- iOS 数据持久化一-属性列表 Plist
- Keepalived实现HA
- rtp协议详解/rtcp协议详解
- 【JavaScript】分秒倒计时器