leetcode brush title notes (Golang) - 83 Remove Duplicates from Sorted List.

83. Remove Duplicates from Sorted List

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

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func deleteDuplicates(head *ListNode) *ListNode {
	if head == nil {
		return head
	}
	dummy := ListNode{Val: 0}
	preNode := &dummy
	node := head
	for node != nil {
		for node.Next!=nil && node.Next.Val == node.Val {
			node = node.Next
		}
		preNode.Next = node
        preNode = node
		node = node.Next
	}
	return dummy.Next
}
Published 65 original articles · won praise 0 · Views 348

Guess you like

Origin blog.csdn.net/weixin_44555304/article/details/104318679