链表_leetcode328

# 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
"""

if not head or not head.next:
return head

head1 = ListNode(0)
head2 = ListNode(0)
p1 = head1
p2 = head2

odd = True

cur = head

while cur:

if odd:
p1.next = cur
p1 = cur
odd = False
else:
p2.next = cur
p2 = cur
odd = True

cur = cur.next


p1.next = head2.next
p2.next = None

return head1.next
posted @ 2019-03-19 11:02  AceKo  阅读(111)  评论(0编辑  收藏  举报