[LeetCode] Remove Duplicates from Sorted List
来源:互联网 发布:阿里云售后电话号码 编辑:程序博客网 时间:2024/06/10 19:29
Problem : Given a sorted linked list, delete all duplicates such that each element appear onlyonce.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
1.C++版
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *deleteDuplicates(ListNode *head) { if(NULL == head){ return NULL; } ListNode *p = head; while(NULL != p->next){ if(p->val == p->next->val){ p->next = p->next->next; }else{ p = p->next; } } return head; }};
2.Java版
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode deleteDuplicates(ListNode head) { if(null == head){ return null; } ListNode p = head; while(null != p.next){ if(p.val == p.next.val){ p.next = p.next.next; }else{ p = p.next; } } return head; }}
3.Python版
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: # @param head, a ListNode # @return a ListNode def deleteDuplicates(self, head): if None == head: return None p = head while None != p.next: if p.val == p.next.val: p.next = p.next.next else: p = p.next return head
完
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
- wpa_supplicant--连接过程分析
- Redis运行流程源码解析
- 绝对路径与相对路径
- Google guava
- 获取当前设备可用内存
- [LeetCode] Remove Duplicates from Sorted List
- 软考之路(四)---软件工程一 概念模型,逻辑模型,物理模型
- Maven工程中资源文件编码问题
- Core文件的分析
- scp和route指令学习
- 继承应用
- 数据结构目标
- Redis源码简要分析
- 华为西安2015实习生机试题基础题--------求输入的五个数字中频率最高的,输出数字和次数