题解 | #删除有序链表中重复的元素-II#

删除有序链表中重复的元素-II

http://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024

用递归

import java.util.*;

/*

  • public class ListNode {
  • int val;
  • ListNode next = null;
  • }
  • /

public class Solution {
/**
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode deleteDuplicates (ListNode head) {
// write code here
if (head == null){return head;}
if (head.next == null){return head;}
if (head.val != head.next.val){
head.next = deleteDuplicates(head.next);
return head;
} else {
int val = head.val;
while (head != null && head.val == val){
head = head.next;
}
return deleteDuplicates(head);
}
}
}

全部评论

相关推荐

已注销:bro不如吃顿疯狂星期四
点赞 评论 收藏
分享
没hc还海面!呜呜,避雷
回收旧报纸:没有海面吧,我做完笔试有一个多月了,还没消息
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务