LeetCode--关于删除排序链表中的重复元素

LeetCode--关于删除排序链表中的重复元素

  • 题目描述
  • 代码

题目描述

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

代码

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class DeletingDuplicates {
	public ListNode deleteDuplicates(ListNode head) {
        ListNode t = head;
        while(t != null && t.next != null)
        {
            if(t.val == t.next.val)
                t.next = t.next.next;
            else
                t = t.next;
        }
        return head;
    }
}

你可能感兴趣的:(OJ,Java刷题,LeetCode)