首页 > 其他 > 详细

leetcode修炼之路——83. Remove Duplicates from Sorted List

时间:2016-08-25 23:55:53      阅读:405      评论:0      收藏:0      [点我收藏+]

哈哈,我又来了。昨天发现题目太简单就没有放上来,今天来了一道有序链表的题。题目如下:

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.

题目分析:简单来说就是去除有序链表中重复的值。

代码实现:

ListNode:

1 public class ListNode {
2     int val;
3     ListNode next;
4 
5     ListNode(int x) {
6         val = x;
7     }
8 }

具体代码实现:

 1 public static ListNode deleteDuplicates(ListNode head) {
 2 
 3         if (head != null) {
 4 
 5             if (head.next != null) {
 6                 if (head.val == head.next.val) {//如果值相等
 7                     head.next = head.next.next;//让他后面的指向后面的后面
 8                     deleteDuplicates(head);//再次进行比较
 9                 } else {
10                     deleteDuplicates(head.next);
11                 }
12             }
13 
14         }
15 
16         return head;
17     }

总体来说还是比较简单,一次ac,哈哈。

 

leetcode修炼之路——83. Remove Duplicates from Sorted List

原文:http://www.cnblogs.com/mc-ksuu/p/5808688.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!