head = None
    place = ln
    if place.nextNode == None:
        return place
    nextplace = place.nextNode
    while nextplace != None:
        nextplace = place.nextNode
        if place.value < x:
            if front == None:
                front = place
                head = front
            else:
                front.nextNode = place
                front = front.nextNode
        else:
            place.nextNode = back
            back = place
        place = nextplace
    front.nextNode = back
    return head
            
a = LN(3)
b = LN(5,a)
c = LN(9,b)
d = LN(2,c)
e = LN(6,d)
printList(e)
print()
printList(partition(e,6))
    
예제 #2
0
        nextNode = nextnext
        nextnext = nextNode.nextNode
        
    nextNode.nextNode = currentNode
    return nextNode



a = LN(1)
b = LN(2)
c = LN(3)

a.nextNode = b
b.nextNode = c

printList(a)
printList(reverse(a))

#I looked at the solution on InterviewCake after doing this, and their
#implementation is simpler to read. Oh well, I'll try harder next time.

'''
    solution from InterviewCake, for my own reference
def reverse(head_of_list):
    current = head_of_list
    previous = None
    next = None
    
    # until we have 'fallen off' the end of the list
    while current:
        
                stack1.append(0)
                
    #creating answer linked list
    carry = 0
    answer = None
    while len(stack1) > 0:
        value = stack1.pop()+stack2.pop()+carry
        carry = value//10
        temp = LN(value%10,answer)
        answer = temp
    if carry != 0:
        temp = LN(carry,answer)
        answer = temp
    return answer


a = LN(6)
b = LN(1,a)
c = LN(7,b) #716

d = LN(2)
e = LN(9,d)
f = LN(5,e) #592

g = sumList(c,f)
printList(g) #1308



            
#Cracking the Coding Interview 4.3
#Given a binary tree, design an algorithm which creates a linked list
#of all the nodes at each depth

#I'm going to store each depth in an index of a list, with a header linked list

from TN import TN, printTree
from LN import LN, printList
from codingInterview42 import minimalTree

def listDepth(TN,DL,index):
    if TN != None:
        DL[index].nextNode = LN(TN.value,DL[index].nextNode)
        listDepth(TN.left,DL,index+1)
        listDepth(TN.right,DL,index+1)

l = [1,2,3,4,5,6,7,8,9]
root = minimalTree(l)
print(printTree(root,""))
DL = [LN(),LN(),LN(),LN()]

listDepth(root,DL,0)

for a in DL:
    printList(a.nextNode)
    print()




#I assume I have the linked list as well as the length

def palindrome(listNode, l: int):
    ln,stack = listNode,[]
    half = ceil(l/2)
    #loading half the nodes into the stack
    for i in range(half):
        stack.append(ln.value)
        ln = ln.nextNode
    #dequeueing if it's an odd number of nodes
    if half%2 == 1:
        stack.pop()
    #checking to see if the second half mirrors the first
    while ln != None:
        if ln.value != stack.pop():
            return False
        ln = ln.nextNode
    return True


a = LN(3)
b = LN(2,a)
c = LN(1,b)
d = LN(2,c)
e = LN(3,d)
f = LN(4,e)

printList(f)
print()
print(palindrome(f,6))