java 83.从Sorted List(1st).java中删除重复项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 83.从Sorted List(1st).java中删除重复项相关的知识,希望对你有一定的参考价值。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode cur = pre.next;
        while (cur != null) {
            while (cur.next != null && cur.val == cur.next.val) {
                cur = cur.next;
            }
            if (pre.next != cur) {
                pre.next = cur;
            }
            pre = pre.next;
            cur = cur.next;
        }
        return dummy.next;
    }
}
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode cur = head;
        while (cur != null && cur.next != null) {
            if (cur.val == cur.next.val) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }
        return head;
    }
}

以上是关于java 83.从Sorted List(1st).java中删除重复项的主要内容,如果未能解决你的问题,请参考以下文章

java 83.从Sorted List(1st).java中删除重复项

java 83.从Sorted List(1st).java中删除重复项

java 83.从Sorted List(1st).java中删除重复项

java 83.从Sorted List(1st).java中删除重复项

83. Remove Duplicates from Sorted List java

[LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序数组中删除重复项)