python 链表的奇偶重排
时间: 2024-01-31 13:11:09 浏览: 36
Python链表的奇偶重排是指将链表中的奇数节点和偶数节点分别排在一起,且保持它们的相对顺序不变。下面是一种实现方式:
1. 定义两个指针,odd指向奇数节点的末尾,even指向偶数节点的末尾。
2. 遍历链表,根据节点的位置将其连接到对应的奇数或偶数节点的末尾。
3. 最后将奇数节点的末尾指向偶数节点的头部,形成新的链表。
下面是Python代码实现:
```python
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def oddEvenList(head):
if not head or not head.next:
return head
odd = head
even = head.next
even_head = even
while even and even.next:
odd.next = even.next
odd = odd.next
even.next = odd.next
even = even.next
odd.next = even_head
return head
```
这样,经过奇偶重排后,链表中的奇数节点和偶数节点会分别排在一起。
相关问题
重排链表python实现
重排链表的python实现可以使用栈来实现。首先,将链表中的节点压入栈中,然后按照指定的规则依次从栈中出栈,重新连接节点。
代码示例如下:
```python
class Solution(object):
def reorderList(self, head):
if not head:
return
stack = []
s = head
while s.next:
stack.append(s.next)
s = s.next
s = head
n = 0
while stack:
if n % 2 == 0:
one = stack.pop()
else:
one = stack.pop(0)
one.next = None
s.next = one
s = s.next
n += 1
```
这段代码中,首先判断头节点是否为空,然后初始化一个栈,并将链表中的其它节点压入栈中。然后,重新遍历链表,根据指定的规则从栈中出栈,并重新连接节点。
这样就能实现重排链表的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [LeetCode: 重排链表 python实现](https://blog.csdn.net/weixin_43786241/article/details/105752892)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [L2-022 重排链表(Python3)](https://blog.csdn.net/weixin_55730361/article/details/126516893)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
python 链表 re
Python中的链表(Linked List)是一种数据结构,它不使用数组连续存储元素,而是通过节点(Node)之间的指针链接在一起。每个节点包含两个部分:存储数据的数据域和指向下一个节点的指针。在Python中,虽然内置的数据结构如列表(List)提供了内置的数组功能,但如果你需要实现链表,可以自定义类来模拟链表的行为。
`re`模块(Regular Expression)是Python标准库的一部分,它用于处理正则表达式(Regular Expressions)。正则表达式是一种强大的文本处理工具,能够用来搜索、替换和提取字符串中的模式。在`re`模块中,你可以执行常见的操作,如匹配(`re.match()`, `re.search()`)、查找所有匹配(`re.findall()`)和替换匹配(`re.sub()`)等。
如果你想要在Python链表中应用正则表达式,例如验证节点数据是否符合特定的格式,你可以在节点的`__init__`方法中使用`re`模块进行检查。不过,链表本身并不会直接和`re`模块关联。
相关推荐
![](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)