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.
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.
1->1->2
1->2
1->1->2->3->3
1->2->3
给定一个排序链表,删除所有重复的元素每个元素只留下一个。
遍历之,遇到当前节点和下一节点的值相同时,删除下一节点,并将当前节点next值指向下一个节点的next, 当前节点首先保持不变,直到相邻节点的值不等时才移动到下一节点。
next
/** * Definition for ListNode * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { /** * @param ListNode head is the head of the linked list * @return: ListNode head of linked list */ public static ListNode deleteDuplicates(ListNode head) { ListNode curr = head; while (curr != null) { while (curr.next != null && curr.val == curr.next.val) { curr.next = curr.next.next; } curr = curr.next; } return head; } }
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8