def removeElements(self, head: ListNode, val: int) -> ListNode:
时间: 2024-05-22 10:11:57 浏览: 108
# If head is None or the val is not in head, return head
if not head:
return head
if head.val == val:
head = head.next
return self.removeElements(head, val)
# Traverse through the list
prev = None
curr = head
while curr:
if curr.val == val:
if prev:
prev.next = curr.next
else:
head = curr.next
curr = curr.next
else:
prev = curr
curr = curr.next
return head
相关问题
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
`removeElements` 函数用于从给定的链表(`head`)中删除所有值等于 `val` 的元素,返回新链表的头节点。这个函数采用迭代方式实现:
1. 创建一个虚拟头节点 `dummy`,并将其 `next` 指向原始链表的头 `head`。
2. 使用一个指针 `cur` 初始化为 `dummy`,然后开始遍历链表。
3. 在每次循环中,检查当前节点 `cur.next` 的值是否等于 `val`。如果相等,则跳过该节点,通过设置 `cur.next` 为 `cur.next.next` 来移除它。
4. 如果不等于 `val`,则简单地移动 `cur` 到下一个节点。
5. 循环结束后,`dummy.next` 就指向了新的链表头,所以返回 `dummy.next`。
代码示例如下[^1]:
```python
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
dummy = ListNode() # 创建虚拟头节点
dummy.next = head # 将 dummy 的 next 指向原头
cur = dummy # 定义当前节点
while cur and cur.next: # 当还有节点时继续
if cur.next.val == val: # 找到要删除的节点
cur.next = cur.next.next # 跳过它
else:
cur = cur.next # 否则,移动到下一个节点
return dummy.next # 返回新链表的头节点
```
用c语言写class ListNode: def __init__(self, x): self.val = x self.next = Nonedef mergeTwoLists(l1: ListNode, l2: ListNode) -> ListNode: if not l1: return l2 if not l2: return l1 if l1.val <= l2.val: head = l1 l1 = l1.next else: head = l2 l2 = l2.next cur = head while l1 and l2: if l1.val <= l2.val: cur.next = l1 l1 = l1.next else: cur.next = l2 l2 = l2.next cur = cur.next if l1: cur.next = l1 else: cur.next = l2 return head# 测试l1 = ListNode(1)l1.next = ListNode(3)l1.next.next = ListNode(5)l2 = ListNode(2)l2.next = ListNode(4)l2.next.next = ListNode(6)head = mergeTwoLists(l1, l2)while head: print(head.val, end=' ') head = head.next
以下是使用C语言实现的代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
if (!l1) return l2;
if (!l2) return l1;
struct ListNode *head, *cur;
if (l1->val <= l2->val) {
head = l1;
l1 = l1->next;
} else {
head = l2;
l2 = l2->next;
}
cur = head;
while (l1 && l2) {
if (l1->val <= l2->val) {
cur->next = l1;
l1 = l1->next;
} else {
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
if (l1) {
cur->next = l1;
} else {
cur->next = l2;
}
return head;
}
int main() {
struct ListNode *l1, *l2, *head;
l1 = (struct ListNode *)malloc(sizeof(struct ListNode));
l1->val = 1;
l1->next = (struct ListNode *)malloc(sizeof(struct ListNode));
l1->next->val = 3;
l1->next->next = (struct ListNode *)malloc(sizeof(struct ListNode));
l1->next->next->val = 5;
l1->next->next->next = NULL;
l2 = (struct ListNode *)malloc(sizeof(struct ListNode));
l2->val = 2;
l2->next = (struct ListNode *)malloc(sizeof(struct ListNode));
l2->next->val = 4;
l2->next->next = (struct ListNode *)malloc(sizeof(struct ListNode));
l2->next->next->val = 6;
l2->next->next->next = NULL;
head = mergeTwoLists(l1, l2);
while (head) {
printf("%d ", head->val);
head = head->next;
}
printf("\n");
return 0;
}
```
输出结果为:1 2 3 4 5 6
阅读全文