将一个混合了字符和数字的链表分成两个链表:字符链表,数字链表
时间: 2024-01-04 15:16:20 浏览: 80
把链表中字符、数字和其他符号分开
好的,可以通过遍历链表,将字符节点和数字节点分别加入到两个新的链表中。具体实现可以参考以下代码:
```python
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def splitList(head: ListNode) -> tuple:
char_head = char_tail = ListNode()
num_head = num_tail = ListNode()
while head:
if isinstance(head.val, str):
char_tail.next = head
char_tail = char_tail.next
else:
num_tail.next = head
num_tail = num_tail.next
head = head.next
char_tail.next = None
num_tail.next = None
return char_head.next, num_head.next
```
阅读全文