-
https://leetcode.com/problems/remove-duplicates-from-sorted-list/
-
head.val 과 head.next.val 을 비교해서 같으면 head.next.next를 head.next에 연결해준다.
Runtime: 0 ms, faster than 100.00% of Java online submissions for Remove Duplicates from Sorted List.
Memory Usage: 36.2 MB, less than 100.00% of Java online submissions for Remove Duplicates from Sorted List.
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode mockHead = new ListNode(-1);
mockHead.next = head;
while(head != null && head.next != null){
if(head.val == head.next.val){
head.next = head.next.next;
continue;
}
head = head.next;
}
return mockHead.next;
}
}
No comments:
Post a Comment