编程是一门需要不断练习的技能,以下是一些手工编程试题,帮助您提升编程能力和解决问题的能力。
请写一个函数,输入一个整型数组,返回去重后的数组,保持原有的顺序。
```python
def remove_duplicates(arr):
result = []
for num in arr:
if num not in result:
result.append(num)
return result
测试
arr = [1, 2, 3, 3, 4, 2, 5]
print(remove_duplicates(arr)) 应该输出 [1, 2, 3, 4, 5]
```
请实现一个函数,将一个单链表进行反转。
```python
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverse_linked_list(head):
prev = None
current = head
while current:
next_node = current.next
current.next = prev
prev = current
current = next_node
return prev
测试
node1 = ListNode(1)
node2 = ListNode(2)
node3 = ListNode(3)
node1.next = node2
node2.next = node3
reversed_head = reverse_linked_list(node1)
while reversed_head:
print(reversed_head.val)
reversed_head = reversed_head.next
```
通过不断练习手工编写这些算法题目,可以帮助您更深入理解数据结构和算法原理,提升编程技能。
文章已关闭评论!
2025-04-05 04:48:22
2025-04-05 04:30:15
2025-04-05 04:11:55
2025-04-05 03:53:53
2025-04-05 03:35:37
2025-04-05 03:17:25
2025-04-05 02:59:13
2025-04-05 02:40:59