标签:输出 lock public 结构 color display node 关于 val
今天我们开始另一模块的算法题,关于链表的操作. Let‘s do it !!!
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次.
示例1
输入: 1->1->2 输出: 1->2
示例2
输入: 1->1->2->3->3 输出: 1->2->3
public class ListNode { public var val: Int? public var next: ListNode? public init(_ val: Int) { self.val = val self.next = nil } } func deleteDuplicates(_ head: ListNode?) -> ListNode? { var current = head var nextPointer = head?.next current?.next = nil while nextPointer != nil { if current?.val == nextPointer?.val { nextPointer = nextPointer?.next } else { current?.next = nextPointer current = current?.next nextPointer = current?.next current?.next = nil //清楚野指针 } } return head }
运行结果如下:
上面就是本题目的思路和代码,可以直接复制粘贴运行,希望对大家有所帮助!!!
标签:输出 lock public 结构 color display node 关于 val
原文地址:https://www.cnblogs.com/guohai-stronger/p/11926125.html