[leetcode, JS] 83. Remove Duplicates from Sorted List

mxxn·2023년 8월 8일
0

leetcode

목록 보기
16/198

문제

문제 링크 : Remove Duplicates from Sorted List

풀이

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function(head) {
    let result = head
    while(result) {
        if(result.next !== null && result.val === result.next.val) {
            result.next = result.next.next
            continue
        }
        result = result.next
    }
    return head;
    
};
  1. result 변수에 head를 담고 result.next가 null이 아니고 다음 값이 반복되는 값이라면 다음 다음 값을 next에 담고
  2. 그게 아니라면 다음값을 담아서 return
  • Runtime 59 ms, Memory 44 MB
profile
내일도 글쓰기

0개의 댓글