本文主要是介绍leetcode83~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.
public class RemoveDuplicatesfromSortedList {//迭代实现public ListNode deleteDuplicates(ListNode head) {if(head==null) return null;ListNode node = head;while(node.next!=null) {if(node.val==node.next.val) {node.next = node.next.next;} else {node = node.next;}}return head;}//递归实现public ListNode deleteDuplicates2(ListNode head) {if(head==null || head.next==null) return head;head.next = deleteDuplicates2(head.next);if(head.val==head.next.val) {return head.next;} else {return head;}}
}
这篇关于leetcode83~Remove Duplicates from Sorted List的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!