机试刷题记录 2023-7-11
发布人:shili8
发布时间:2024-11-01 14:12
阅读次数:0
**机试刷题记录**
**日期:**2023-7-11**题目1:**
### 题目描述给定一个链表,链表中每个结点的值都是一个整数。请实现一个函数,返回链表中所有结点的值之和。
### 输入输出示例输入:`1 ->2 ->3`
输出:`6`
###代码实现
class ListNode: def __init__(self, x): self.val = x self.next = Nonedef get_sum(head): """ Returns the sum of all values in a linked list. Args: head (ListNode): The head node of the linked list. Returns: int: The sum of all values in the linked list. """ total =0 current = head # Traverse the linked list and add each value to the total while current is not None: total += current.val current = current.next return total# Example usage: head = ListNode(1) head.next = ListNode(2) head.next.next = ListNode(3) print(get_sum(head)) # Output:6
**题目2:**
### 题目描述给定一个链表,链表中每个结点的值都是一个整数。请实现一个函数,返回链表中所有结点的值之和,并且将链表中的所有结点的值都加上1。
### 输入输出示例输入:`1 ->2 ->3`
输出:`7`
###代码实现
class ListNode: def __init__(self, x): self.val = x self.next = Nonedef get_sum_and_increment(head): """ Returns the sum of all values in a linked list and increments each value by1. Args: head (ListNode): The head node of the linked list. Returns: int: The sum of all values in the linked list after incrementing each value by1. """ total =0 current = head # Traverse the linked list, add each value to the total and increment it by1 while current is not None: total += (current.val +1) current = current.next return total# Example usage: head = ListNode(1) head.next = ListNode(2) head.next.next = ListNode(3) print(get_sum_and_increment(head)) # Output:12
**题目3:**
### 题目描述给定一个链表,链表中每个结点的值都是一个整数。请实现一个函数,返回链表中所有结点的值之和,并且将链表中的所有结点的值都乘以2。
### 输入输出示例输入:`1 ->2 ->3`
输出:`6`
###代码实现
class ListNode: def __init__(self, x): self.val = x self.next = Nonedef get_sum_and_multiply(head): """ Returns the sum of all values in a linked list and multiplies each value by2. Args: head (ListNode): The head node of the linked list. Returns: int: The sum of all values in the linked list after multiplying each value by2. """ total =0 current = head # Traverse the linked list, add each value to the total and multiply it by2 while current is not None: total += (current.val *2) current = current.next return total# Example usage: head = ListNode(1) head.next = ListNode(2) head.next.next = ListNode(3) print(get_sum_and_multiply(head)) # Output:12
**题目4:**
### 题目描述给定一个链表,链表中每个结点的值都是一个整数。请实现一个函数,返回链表中所有结点的值之和,并且将链表中的所有结点的值都加上1,然后将链表中的所有结点的值都乘以2。
### 输入输出示例输入:`1 ->2 ->3`
输出:`14`
###代码实现
class ListNode: def __init__(self, x): self.val = x self.next = Nonedef get_sum_and_multiply(head): """ Returns the sum of all values in a linked list and multiplies each value by2. Args: head (ListNode): The head node of the linked list. Returns: int: The sum of all values in the linked list after multiplying each value by2. """ total =0 current = head # Traverse the linked list, add each value to the total and multiply it by2 while current is not None: total += (current.val *2) current = current.next return total# Example usage: head = ListNode(1) head.next = ListNode(2) head.next.next = ListNode(3) print(get_sum_and_multiply(head)) # Output:12
**题目5:**
### 题目描述给定一个链表,链表中每个结点的值都是一个整数。请实现一个函数,返回链表中所有结点的值之和,并且将链表中的所有结点的值都加上1,然后将链表中的所有结点的值都乘以2。
### 输入输出示例输入:`1 ->2 ->3`
输出:`14`
###代码实现
class ListNode: def __init__(self, x): self.val = x self.next = Nonedef get_sum_and_multiply(head): """ Returns the sum of all values in a linked list and multiplies each value by2. Args: head (ListNode): The head node of the linked list. Returns: int: The sum of all values in the linked list after multiplying each value by2. """ total =0 current = head # Traverse the linked list, add each value to the total and multiply it by2 while current is not None: total += (current.val *2) current = current.next return total# Example usage: head = ListNode(1) head.next = ListNode(2) head.next.next = ListNode(3) print(get_sum_and_multiply(head)) # Output:12
以上是本次机试刷题记录的内容。希望这些代码示例和注释能够帮助你更好地理解链表相关的问题和解决方案。