Go 双指针
package main import . "nc_tools" /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * @param head ListNode类 * @return ListNode类 */ func deleteDuplicates( head *ListNode ) *ListNode { // write code here if head == nil || head.Next == nil { return head } // 设置空的头节点 newHead := &ListNode{Next: head} slow, fast := newHead, head // for fast != nil && fast.Next != nil { // 当遇到当前节点值和下一节点值相等的节点时 // 进行while循环找到下一个不相等的节点,挂到slow节点上 if fast.Next.Val == fast.Val { node := fast for node != nil && node.Val == fast.Val { node = node.Next } slow.Next = node fast = node }else{ // 当遇到当前节点值和下一节点值不相等的节点时 // slow和fast都移动到下一个节点接着遍历就行 slow = slow.Next fast = fast.Next } } return newHead.Next }