Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given1->1->2
, return1->2
.
Given1->1->2->3->3
, return1->2->3
.
给定一个排序链表,删除所有重复的元素每个元素只留下一个。
遍历之,遇到当前节点和下一节点的值相同时,删除下一节点,并将当前节点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;
}
}