Ejemplo n.º 1
0
def insertSortedList(head, item):
    predNode = None
    curNode = head

    while curNode is not None and curNode.item < item:
        predNode = curNode
        curNode = curNode.next

    newNode = ListNode(item)
    newNode.next = curNode

    if curNode is head:
        head = curNode
    else:
        predNode.next = newNode
Ejemplo n.º 2
0
def appendTail(head, tail, item):
    newNode = ListNode(item)

    if head is None:
        head = newNode
    else:
        tail.next = newNode

    tail = newNode
from usingTailReference import ListNode


def removeAll(head):
    curNode = head

    while curNode is not None:
        succNode = curNode.next
        curNode.next = None
        curNode.item = None
        curNode = None
        curNode = succNode


a = ListNode("a")
b = ListNode("b")
a.next = b

removeAll(a)

print(a)
print("is a == None", a is None)

print(a.item)
from usingTailReference import ListNode

head = ListNode(73)
curNode = ListNode(2)
head.next = curNode

tempNode = ListNode(52)
curNode.next = tempNode

tempNode.next = ListNode(18)
tempNode.next.next = ListNode(36)

print(head.item)
print(head.next.item)
print(head.next.next.item)
print(head.next.next.next.item)
print(head.next.next.next.next.item)

curNode.next.next = curNode.next.next.next

print()
print(head.item)
print(head.next.item)
print(head.next.next.item)
print(head.next.next.next.item)
# print(head.next.next.next.next.item)

Ejemplo n.º 5
0
from usingTailReference import ListNode

def splitInHalf(head):
    curNode = head
    headList = list()

    while curNode is not None:
        headList.append(curNode)
        curNode = curNode.next

    return headList[len(headList) // 2]

a = ListNode("a")
b = ListNode("b")
a.next = b

print(splitInHalf(a).item)

c = ListNode("c")
b.next = c

print(splitInHalf(a).item)
Ejemplo n.º 6
0
from usingTailReference import ListNode

box = None
temp = None

for i in range( 4 ) :
    if i % 3 != 0 :
        temp = ListNode( i )
        temp.next = box
        box = temp

print(box)
print(box.item)
print(box.next.item)
print(box.next.next.item)
Ejemplo n.º 7
0
from usingTailReference import ListNode


def insertSortedList(head, item):
    predNode = None
    curNode = head

    while curNode is not None and curNode.item < item:
        predNode = curNode
        curNode = curNode.next

    newNode = ListNode(item)
    newNode.next = curNode

    if curNode is head:
        head = curNode
    else:
        predNode.next = newNode


a = ListNode("a")
c = ListNode("c")

a.next = c

insertSortedList(a, "b")

print(a.item)
print(a.next.item)
print(a.next.next.item)
from usingTailReference import ListNode

head = ListNode(73)
curNode = ListNode(2)
head.next = curNode

tempNode = ListNode(52)
curNode.next = tempNode

tempNode.next = ListNode(18)
tempNode.next.next = ListNode(36)

print(head.item)
print(head.next.item)
print(head.next.next.item)
print(head.next.next.next.item)
print(head.next.next.next.next.item)

newNode = ListNode("22")
newNode.next = curNode.next
curNode.next = newNode

print()
print(head.item)
print(head.next.item)
print(head.next.next.item)
print(head.next.next.next.item)
print(head.next.next.next.next.item)
print(head.next.next.next.next.next.item)
Ejemplo n.º 9
0
from usingTailReference import ListNode


def sortedSearch(head, target):
    curNode = head

    while curNode is not None and curNode.item <= target:
        if curNode.item == target:
            return True
        else:
            curNode = curNode.next

    return False


a = ListNode(1)
b = ListNode(2)

a.next = b

print(sortedSearch(a, 2))