自己1:首先将链表分奇偶位置分别将原链表分开成为两个链表,然后将偶数链表拼接在奇数链表之后,让后返回拼接后的链表即可。
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def oddEvenList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
head_ji = ListNode(-1)
tail_ji = head_ji
head_ou = ListNode(-1)
tail_ou = head_ou
p = head
count = 1
while p:
tmp = p
p = p.next
tmp.next = None
if count%2:
tail_ji.next = tmp
tail_ji = tail_ji.next
count += 1
else:
tail_ou.next = tmp
tail_ou = tail_ou.next
count += 1
tail_ji.next = head_ou.next
tail_ou.next = None
return head_ji.next