Clear Python Solution

• ``````def oddEvenList(self, head):
dummy1 = odd = ListNode(0)
dummy2 = even = ListNode(0)
odd = odd.next
even = even.next
odd.next = dummy2.next
return dummy1.next``````

• can u tell me what is the meaning of" odd = ListNode(0) "? and why do u assign "odd.next = dummy2.next"? and return dummy1.next?
Thank you very much

• head became None at the end

• I try to explain your questions:
The author solved this problem by using two linked lists, odd chain and even chain. "odd = ListNode(0)" and "even = ListNode(0)" get two nodes to build these two lists
dummy1 is the head of odd lists and dummy2 is even, after the "while" statements odd is the tail of odd list, "odd.next = dummy2.next" is used to link two chains
Even dummy1 is odd chain's head, but it's a "blank node", that is its val is meaningless, actually ,"dummy1.next" is the real head node, and it equals to the first node in the chain which question given us

• thank you so much, but "dummy.next" should be one number? how could it be a list?

• Actually, dummy is a node, and dummy.next is so. If you catch a node, then you catch all the nodes that after it, they are linked by "next" pointer, you can get the second node by next, third by next.next, and so on. So I said you get a list
Forgive my broken English =.=

• Thank you for the code. I have a bit different question here. In Leetcode they enter the input as an array and it worked for them. However, when I try to run that same code in local machine, that input is no longer valid. Can anybody points out, how to give a linked list input.

• You can construct the linked list similar to the code above:

``````def construct_linked_list(arr):
input = dummy = ListNode(0)
for num in arr:
dummy.next = ListNode(num)
dummy = dummy.next
return input.next``````

• My first 96.38%, new to leetcode. Any suggestion is welcome.

``````class Solution(object):
if head is None: return None
p = o.next
while p.next is not None:
#t = o.next
o.next = p.next
p.next = p.next.next
#o.next.next = t
o = o.next
p = p.next
if p is None: break
#if o is None: break
``````

• ANy idea why this solution is not working:

``````# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
"""
:rtype: ListNode
"""

while e and e.next:
o.next = o.next.next
e.next = e.next.next
o = o.next
e = e.next

``````

• @modqhx said in Clear Python Solution:

ANy idea why this solution is not working:

Well, first, I'm very impressed by the elegance of your solution - I have more None checks in mine, which evidently are superfluous.

However, the one explicit None check you do have (presumably to protect from an empty input list) is the source of all your trouble: it instead protects from all non-empty lists. Remove the `not` and you're good to go!

• @modqhx

I think o.next = o.next.next sets the o.next pointer and e.next is actually already pointing to o. So e.next points to the newly set o.next which is an odd numbered element hence creating the problem. So I think one could do:
temp1 = o.next.next
temp2 = e.next.next
o.next = temp1
e.next = temp2

to solve the problem

• This is just brilliant, thx for your solution.

• @tusizi said in Clear Python Solution:

``````    head = head.next.next if even else None
``````

• Even easier solution. Not sure if follows the in place requirement, however.

``````class Solution(object):
"""
:rtype: ListNode
"""
d1=odd=ListNode(0)
d2=even=ListNode(0)
i=1