做链表的一些看起来很乱的题目:
那就将这个链表分割成两部分,试着同时操作两个链表再拼起来。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def oddEvenList(self, head: ListNode) -> ListNode:
if head is None or head.next is None:
return head
enevhead=head.next
odd,enev=head,enevhead
while enev and enev.next:
odd.next=enev.next
odd=odd.next
enev.next=odd.next
enev=enev.next
odd.next=enevhead
return head