def removeElements(self, head: ListNode, val: int) -> ListNode:
时间: 2024-05-22 11:11:57 浏览: 20
# 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
相关问题
用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
1.给定一个单链表的头结点head,实现一个调整单链表的函数,使得k个结点之 间逆序,如果最后一组不够k个节点,则不调整最后几个结点。要求:如果链 表的长度为N;时间复杂度达到O(N)。 例如: 链表:1->2->3->4->5->6->7->8->nul;k=3。 调整后:3-≥2-≥1-5->5-≥4-≥7->8->nu1l。并用主函数验证
可以使用以下代码实现:
```python
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverseKGroup(head: ListNode, k: int) -> ListNode:
if not head or k == 1:
return head
dummy = ListNode(0)
dummy.next = head
pre = dummy
cur = head
i = 0
while cur:
i += 1
if i % k == 0:
pre = reverse(pre, cur.next)
cur = pre.next
else:
cur = cur.next
return dummy.next
def reverse(pre: ListNode, nxt: ListNode) -> ListNode:
last = pre.next
cur = last.next
while cur != nxt:
last.next = cur.next
cur.next = pre.next
pre.next = cur
cur = last.next
return last
```
可以使用以下代码验证:
```python
def printList(head: ListNode):
while head:
print(head.val, end='->')
head = head.next
print('null')
head = ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5, ListNode(6, ListNode(7, ListNode(8))))))))
k = 3
printList(head)
head = reverseKGroup(head, k)
printList(head)
```
输出结果为:
```
1->2->3->4->5->6->7->8->null
3->2->1->6->5->4->7->8->null
```
这个函数可以将链表中每k个节点进行逆序操作,时间复杂度为O(N)。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)