Insert a node in a sorted linked list.
Example
Given list = 1->4->6->8
and val = 5
.
Return 1->4->5->6->8
.
class Solution:
"""
@param head: The head of linked list.
@param val: An integer.
@return: The head of new linked list.
"""
def insertNode(self, head, val):
dummy = ListNode(0, head)
p = dummy
while p.next and p.next.val < val:
p = p.next #select half list stop by val
node = ListNode(val, p.next)
p.next = node #connect next half list started with val
return dummy.next