Content-Length: 4605 | pFad | https://baffinlee.com/leetcode-javascript/problem/remove-duplicates-from-sorted-list.html

Remove Duplicates from Sorted List - LeetCode javascript solutions

83. Remove Duplicates from Sorted List

Difficulty:
Related Topics:
Similar Questions:

Problem

Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3

Solution

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function(head) {
  var now = head;
  while (now) {
    if (now.next && now.next.val === now.val) {
      now.next = now.next.next;
    } else {
      now = now.next;
    }
  }
  return head;
};

Explain:

nope.

Complexity:









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://baffinlee.com/leetcode-javascript/problem/remove-duplicates-from-sorted-list.html

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy