RemoveDuplicatesFromSortedList

  • 2022-12-14
  • 浏览 (514)

RemoveDuplicatesFromSortedList.java 源码

package datastructure.linkedlist.leetcode;

/**
 * @author roseduan
 * @time 2020/11/17 12:17 下午
 * @description 删除排序链表中的重复元素
 */
public class RemoveDuplicatesFromSortedList {

    public ListNode deleteDuplicates(ListNode head) {
        ListNode p = head;
        while (p != null && p.next != null) {
            if (p.val == p.next.val) {
                p.next = p.next.next;
            } else {
                p = p.next;
            }
        }
        return head;
    }

    private static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }
}

你可能感兴趣的文章

AddTwoNumbers

IntersectionLinkedList

LinkedListCycle

0  赞