Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.
Example 1:
Input: head = [1,2,3,3,4,4,5]
Output: [1,2,5]
Example 2:
Input: head = [1,1,1,2,3]
Output: [2,3]
Constraints:
The number of nodes in the list is in the range [0, 300].
-100 <= Node.val <= 100
The list is guaranteed to be sorted in ascending order.
Solution
Approach #0
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */funcdeleteDuplicates(head *ListNode) *ListNode {returndfs(head, -101)}funcdfs(cur *ListNode, val int) *ListNode {if cur ==nil {returnnil }if cur.Val == val || (cur.Next !=nil&& cur.Next.Val == cur.Val) {returndfs(cur.Next, cur.Val) } cur.Next =dfs(cur.Next, cur.Val)return cur}
Approach #1
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */funcdeleteDuplicates(head *ListNode) *ListNode {if head ==nil {returnnil } newHead :=&ListNode{Next: head} cur := newHeadfor cur.Next !=nil&& cur.Next.Next !=nil {if cur.Next.Val == cur.Next.Next.Val { v := cur.Next.Valfor cur.Next !=nil&& cur.Next.Val == v { cur.Next = cur.Next.Next } } else { cur = cur.Next } }return newHead.Next}
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
You are given the root of a binary tree where each node has a value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit.
For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.
For all leaves in the tree, consider the numbers represented by the path from the root to that leaf. Return the sum of these numbers.
The test cases are generated so that the answer fits in a 32-bits integer.
The number of nodes in the tree is in the range [1, 1000].
Node.val is 0 or 1.
Solution
Approach #0
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */funcsumRootToLeaf(root *TreeNode) int {returnsum(root, 0)}funcsum(node *TreeNode, num int) int {if node.Left !=nil&& node.Right !=nil {returnsum(node.Left, num<<1+node.Val) +sum(node.Right, num<<1+node.Val) }if node.Left !=nil {returnsum(node.Left, num<<1+node.Val) }if node.Right !=nil {returnsum(node.Right, num<<1+node.Val) }return num<<1+ node.Val}
Approach #1
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */funcsumRootToLeaf(root *TreeNode) int {var val, res intvar stack []*TreeNodevar pre *TreeNodefor root !=nil||len(stack) >0 {for root !=nil { val = val<<1| root.Val stack =append(stack, root) root = root.Left } root = stack[len(stack)-1]if root.Right ==nil|| root.Right == pre {if root.Left ==nil&& root.Right ==nil { res = res + val } val = val >>1 stack = stack[:len(stack)-1] pre = root root =nil } else { root = root.Right } }return res}