CODE 51: Remove Duplicates from Sorted List

来源:互联网 发布:影吧点播软件 编辑:程序博客网 时间:2024/05/01 03:30

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 ListNode deleteDuplicates(ListNode head) {// Start typing your Java solution below// DO NOT write main() functionif (null == head) {return null;}ListNode node = new ListNode(head.val);ListNode next = head.next;ListNode currentNode = node;while (null != next) {if (next.val > currentNode.val) {currentNode.next = new ListNode(next.val);currentNode = currentNode.next;} else {next = next.next;}}return node;}