day150-LeetCode 83. Remove Duplicates from Sorted List

题目

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.

分析

题目意思是移除一个有序单链表中重复的节点,根据单链表的特点next指针,可以判断相邻连续的节点是否相等。

题解

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
function deleteDuplicates(head) {
if (head === null || head.next === null) return
let cur = head
while(cur.next !== null) {
// 相邻连续节点是否相等
if (cur.val === cur.next.val) {
cur.next = cur.next.next
} else {
cur = cur.next
}
}
return head
}
文章作者: lmislm
文章链接: http://lmislm.com/2019/06/12/2019-06-12/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 LMISLMのBlog