def reverseList(L1):
    reverseL1 = LinkedList()
    current = L1.head
    while current != None:
        reverseL1.addNode(current.value)
        current = current.next
    return reverseL1
Exemplo n.º 2
0
def reverseList(L1):
	reverseL1 = LinkedList()
	current = L1.head
	while current != None:
		reverseL1.addNode(current.value)
		current = current.next
	return reverseL1
Exemplo n.º 3
0
def addLists_rev(L1, L2):
    p1 = L1.head
    p2 = L2.head
    carry = 0
    linkedlist_sum = LinkedList()
    while (p1 != None) or (p2 != None) or (carry != 0):
        dig_sum = carry
        if p1 != None:
            dig_sum += p1.value
            p1 = p1.next
        if p2 != None:
            dig_sum += p2.value
            p2 = p2.next
        linkedlist_sum.addNode(dig_sum % 10)
        carry = dig_sum / 10
    return linkedlist_sum
def addLists_rev(L1, L2):
    p1 = L1.head
    p2 = L2.head
    carry = 0
    linkedlist_sum = LinkedList()
    while (p1 != None) or (p2 != None) or (carry != 0):
        dig_sum = carry
        if p1 != None:
            dig_sum += p1.value
            p1 = p1.next
        if p2 != None:
            dig_sum += p2.value
            p2 = p2.next
        linkedlist_sum.addNode(dig_sum%10)
        carry = dig_sum/10
    return linkedlist_sum
Exemplo n.º 5
0
    current = linkedlist.head
    while current != None:
        length += 1
        current = current.next
    return length



# -------------------test------------------
L1 = randomLinkedList(3, 3, 4)
print "L2:", L1
print "isPalindrome_iter: ", isPalindrome_iter(L1)
print "isPalindrome_recu: ", isPalindrome_recu(L1)
L2 = LinkedList()
for i in range(1,4):
    L2.addNode(i)
for i in range(3, 0, -1):
    L2.addNode(i)
print "L3:", L2
print "isPalindrome_iter: ", isPalindrome_iter(L2)
print "isPalindrome_recu: ", isPalindrome_recu(L2)

# Another method: reverse the list and check if they are the same
def isPalindrome(L1):
	reverseL1 = reverseList(L1)
	return isEqual(L1, reverseL1)

def reverseList(L1):
	reverseL1 = LinkedList()
	current = L1.head
	while current != None:
Exemplo n.º 6
0
        return None

    # Move one runner to head. Making them move at same pace, they will meet at the beginning of the loop
    fast = linkedlist.head
    while fast != slow:
        slow = slow.next
        fast = fast.next

    return fast


# -----------------test------------------
nodes_number = 100
nodes_in_loop = 20
L = LinkedList()
current = L.head
store = []  # store nodes to help creating loop

# Create a linked list
for i in range(nodes_number):
    L.addNode(i)
    current = L.head if i == 0 else current.next
    store.append(current)

# Creat loop
current.next = None if nodes_in_loop <= 0 else store[nodes_number -
                                                     nodes_in_loop]

beginning = findBeginning(L)
print beginning  # 80
Exemplo n.º 7
0
from classes.LinkedList import *

def isPalindrome(linkedlist):
    if linkedlist.head == None:
        return None
    fast = linkedlist.head
    slow = linkedlist.head
    firsthalf = []
    while fast != None and fast.next != None:
        firsthalf.append(slow.value)
        slow = slow.next
        fast = fast.next.next
    if fast != None:
        slow = slow.next
    while slow != None:
        if firsthalf.pop() != slow.value:
            return False
        else:
            slow = slow.next
    return True
L1 = randomLinkedList(3, 3, 4)
print "L2:", L1
print "Is Palindrome? ", isPalindrome(L1)
L2 = LinkedList()
for i in range(1,4):
    L2.addNode(i)
for i in range(3, 0, -1):
    L2.addNode(i)
print "L3:", L2
print "Is Palindrome? ", isPalindrome(L2)
Exemplo n.º 8
0
        return None

    # Move one runner to head. Making them move at same pace, they will meet at the beginning of the loop
    fast = linkedlist.head
    while fast != slow:
        slow = slow.next
        fast = fast.next

    return fast



# -----------------test------------------
nodes_number = 100
nodes_in_loop = 20
L = LinkedList()
current = L.head
store = []                  # store nodes to help creating loop

# Create a linked list
for i in range(nodes_number):
    L.addNode(i)
    current = L.head if i==0 else current.next
    store.append(current)

# Creat loop
current.next = None if nodes_in_loop <= 0 else store[nodes_number - nodes_in_loop]

beginning = findBeginning(L)
print beginning              # 80
def randomLinkedList(length, min, max):
    linkedlist = LinkedList()
    for i in range(length):
        value = randint(min, max)
        linkedlist.addNode(value)
    return linkedlist