The combined list of two ordered lists

Look at the subject:

leetcode 21 Simple

 

 On the code:

. 1  class ListNode:
 2      "", " node " "" 
3      # initialize node 
. 4      DEF  the __init__ (Self, X):
 . 5          self.val = X
 . 6          self.next = None
 . 7          
. 8  class Solution:
 . 9      DEF mergelist (Self, L1, L2):
 10          "" " 
. 11          : L1 the node
 12 is          : L2 the node
 13 is          " "" 
14          # Maintain AN Ahead of the unchanging Reference node to node the return 
15          # lists the head node is defined, for returning the combined 
16         = head ListNode (0)
 . 17          
18 is          # point to the head node 
. 19          CUR = head
 20 is          # determines whether two empty list the list 
21 is          the while L1 IS  Not None and L2 IS  Not None:
 22 is              IF l1.val < l2.val:
 23 is                  cur.next, L1 = L1, l1.next
 24              the else :
 25                  cur.next, L2 = L2, l2.next
 26 is              # move the pointer to the node is added 
27              CUR = cur.next
 28         if l1 is not None:
29             cur.next = l1
30         else:
31             cur.next = l2
32             
33         return head.next
34         

 

Guess you like

Origin www.cnblogs.com/xiaowei2092/p/11720196.html