diff --git a/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README.md b/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README.md index 64d18981736ca..6cf1c39444dcf 100644 --- a/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README.md +++ b/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README.md @@ -76,11 +76,11 @@ tags: ### 方法一:遍历链表 -我们用变量 `ans` 记录当前的十进制值,初始值为 $0$。 +我们用变量 $\textit{ans}$ 记录当前的十进制值,初始值为 $0$。 -遍历链表,对于每个结点,将 `ans` 左移一位,然后再或上当前结点的值。遍历结束后,`ans` 即为十进制值。 +遍历链表,对于每个结点,将 $\textit{ans}$ 左移一位,然后再或上当前结点的值。遍历结束后,$\textit{ans}$ 即为十进制值。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为链表的长度。 +时间复杂度 $O(n)$,其中 $n$ 为链表的长度。空间复杂度 $O(1)$。 @@ -212,12 +212,11 @@ function getDecimalValue(head: ListNode | null): number { // } // } impl Solution { - pub fn get_decimal_value(head: Option>) -> i32 { + pub fn get_decimal_value(mut head: Option>) -> i32 { let mut ans = 0; - let mut cur = &head; - while let Some(node) = cur { + while let Some(node) = head { ans = (ans << 1) | node.val; - cur = &node.next; + head = node.next; } ans } @@ -247,6 +246,31 @@ var getDecimalValue = function (head) { }; ``` +#### C# + +```cs +/** + * Definition for singly-linked list. + * public class ListNode { + * public int val; + * public ListNode next; + * public ListNode(int val=0, ListNode next=null) { + * this.val = val; + * this.next = next; + * } + * } + */ +public class Solution { + public int GetDecimalValue(ListNode head) { + int ans = 0; + for (; head != null; head = head.next) { + ans = ans << 1 | head.val; + } + return ans; + } +} +``` + #### PHP ```php @@ -267,13 +291,12 @@ class Solution { * @return Integer */ function getDecimalValue($head) { - $rs = []; - while ($head != null) { - array_push($rs, $head->val); + $ans = 0; + while ($head !== null) { + $ans = ($ans << 1) | $head->val; $head = $head->next; } - $rsStr = implode($rs); - return bindec($rsStr); + return $ans; } } ``` diff --git a/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README_EN.md b/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README_EN.md index 3e49f84db6367..5d6aaab02c5ac 100644 --- a/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README_EN.md +++ b/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README_EN.md @@ -56,7 +56,13 @@ tags: -### Solution 1 +### Solution 1: Traverse the Linked List + +We use a variable $\textit{ans}$ to record the current decimal value, with an initial value of $0$. + +Traverse the linked list. For each node, left-shift $\textit{ans}$ by one bit, then perform a bitwise OR with the current node's value. After traversal, $\textit{ans}$ is the decimal value. + +The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$. @@ -188,12 +194,11 @@ function getDecimalValue(head: ListNode | null): number { // } // } impl Solution { - pub fn get_decimal_value(head: Option>) -> i32 { + pub fn get_decimal_value(mut head: Option>) -> i32 { let mut ans = 0; - let mut cur = &head; - while let Some(node) = cur { + while let Some(node) = head { ans = (ans << 1) | node.val; - cur = &node.next; + head = node.next; } ans } @@ -223,6 +228,31 @@ var getDecimalValue = function (head) { }; ``` +#### C# + +```cs +/** + * Definition for singly-linked list. + * public class ListNode { + * public int val; + * public ListNode next; + * public ListNode(int val=0, ListNode next=null) { + * this.val = val; + * this.next = next; + * } + * } + */ +public class Solution { + public int GetDecimalValue(ListNode head) { + int ans = 0; + for (; head != null; head = head.next) { + ans = ans << 1 | head.val; + } + return ans; + } +} +``` + #### PHP ```php @@ -243,13 +273,12 @@ class Solution { * @return Integer */ function getDecimalValue($head) { - $rs = []; - while ($head != null) { - array_push($rs, $head->val); + $ans = 0; + while ($head !== null) { + $ans = ($ans << 1) | $head->val; $head = $head->next; } - $rsStr = implode($rs); - return bindec($rsStr); + return $ans; } } ``` diff --git a/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/Solution.cs b/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/Solution.cs new file mode 100644 index 0000000000000..bbfcf9dbff062 --- /dev/null +++ b/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/Solution.cs @@ -0,0 +1,20 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * public int val; + * public ListNode next; + * public ListNode(int val=0, ListNode next=null) { + * this.val = val; + * this.next = next; + * } + * } + */ +public class Solution { + public int GetDecimalValue(ListNode head) { + int ans = 0; + for (; head != null; head = head.next) { + ans = ans << 1 | head.val; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/Solution.php b/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/Solution.php index 7ddaf720fe6ae..514bc47573ca4 100644 --- a/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/Solution.php +++ b/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/Solution.php @@ -15,12 +15,11 @@ class Solution { * @return Integer */ function getDecimalValue($head) { - $rs = []; - while ($head != null) { - array_push($rs, $head->val); + $ans = 0; + while ($head !== null) { + $ans = ($ans << 1) | $head->val; $head = $head->next; } - $rsStr = implode($rs); - return bindec($rsStr); + return $ans; } } \ No newline at end of file diff --git a/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/Solution.rs b/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/Solution.rs index 7c122a66fc0df..b3f205ea5f95c 100644 --- a/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/Solution.rs +++ b/solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/Solution.rs @@ -15,12 +15,11 @@ // } // } impl Solution { - pub fn get_decimal_value(head: Option>) -> i32 { + pub fn get_decimal_value(mut head: Option>) -> i32 { let mut ans = 0; - let mut cur = &head; - while let Some(node) = cur { + while let Some(node) = head { ans = (ans << 1) | node.val; - cur = &node.next; + head = node.next; } ans }