`
zhang_xzhi_xjtu
  • 浏览: 536777 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

[leetcode] RemoveDuplicatesfromSortedList

阅读更多
package leetcode;

/**
* <pre>
* 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.
* </pre>
*/
public class RemoveDuplicatesfromSortedList {
    public class ListNode {
        int      val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            if (head == null)
                return null;

            for (ListNode pre = head, node = head.next; node != null;) {
                if (node.val == pre.val) {
                    pre.next = node.next;
                    node = node.next;
                    continue;
                }
                pre = node;
                node = node.next;
            }
            return head;
        }
    }
}
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics