-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo1290_ListNode.cs
53 lines (51 loc) · 1.89 KB
/
No1290_ListNode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using LeetCode.ExtensionFunction;
using System;
using System.Collections.Generic;
using System.Text;
namespace LeetCode_1290
{
//static void Main(string[] args)
//{
// var solution = new Solution();
// while (true)
// {
// //int input = int.Parse(Console.ReadLine());
// //int input2 = int.Parse(Console.ReadLine());
// //string input = Console.ReadLine();
// //string input2 = Console.ReadLine();
// //int[] intArr = input.Split(',').Select(s => int.Parse(s)).ToArray();
// //int input2 = int.Parse(Console.ReadLine());
// var builder = new DataStructureBuilder();
// //int?[] data = new int?[] { 10, 5, 15, null, null, 6, 20 };
// //int?[] data = new int?[] { -10, 9, 20, null, null, 15, 7 };
// //int?[] data = new int?[] { -2147483648, null, 2147483647 };
// //int?[] data = new int?[] { 1, 3, null, null, 2 };
// //var tree = builder.BuildTree(data);
// var listNode = builder.BuildListNode(new int[] { 1, 0, 1 });
// //var listNode2 = builder.BuildListNode(new int[] { 5, 6});
// //listNode2.next.next = listNode.next.next.next.next;
// var res = solution.GetDecimalValue(listNode);
// ConsoleX.WriteLine(res);
// }
//}
public class Solution
{
/// <summary>
/// 链表移动 + 左移位运算(乘法也可以,乘以2也是一样的道理)
/// 时间复杂度:O(n)
/// 空间复杂度:O(1)
/// </summary>
/// <param name="head"></param>
/// <returns></returns>
public int GetDecimalValue(ListNode head)
{
int res = 0;
while (head != null)
{
res = (res << 1) + head.val;
head = head.next;
}
return res;
}
}
}