Exemplo n.º 1
0
import initialize

# Program to delete m nodes after n nodes

def delete_m_after_n(head, m, n):
	count_n = 0
	curr = head
			
	while ((count_n < n-1) and (curr != None)):
		curr = curr.nextnode
		count_n += 1

	temp = curr

	count = 0

	while count < m and curr != None:
		curr = curr.nextnode
		count += 1

	temp.nextnode = curr.nextnode


if __name__=="__main__":
	ll = initialize.initialize_linked_list()

	delete_m_after_n(ll.head, 2, 3)

	ll.print_list()
	y_prev = None

	for i in range(count-k):
		y_prev = y
		y = y.nextnode

	if x_prev:
		x_prev.nextnode = y

	if y_prev:
		y_prev.nextnode = x

	temp = x.nextnode
	x.nextnode = y.nextnode
	y.nextnode = temp

	if k == 1:
		lList.head = y
	if k == count:
		lList.head = x

	return lList.head

lList = initialize.initialize_linked_list()

head = swap(lList, 1)

while (head):
	print head.data
	head = head.nextnode
    slow_ptr = head
    while fast_ptr and fast_ptr.nextnode:
        prev_slow_ptr = slow_ptr
        slow_ptr = slow_ptr.nextnode
        fast_ptr = fast_ptr.nextnode.nextnode

    if fast_ptr:
        mid_point = slow_ptr
        slow_ptr = slow_ptr.nextnode

    second_half = slow_ptr
    prev_slow_ptr.next = None
    # Reverse second half of linked list
    head_second_half = reverse(second_half)
    # Compare two lists if they are equal or not
    result = compare(head, head_second_half)

    head_second_half = reverse(head_second_half)

    if mid_point:
        slow_ptr.nextnode = mid_point
        mid_point.nextnode = head_second_half
    else:
        prev_slow_ptr.nextnode = second_half

    return result


head = initialize.initialize_linked_list()
print check_palindrome(head)
Exemplo n.º 4
0
	temp1 = head1
	temp2 = head2
	prev1 = None

	while (temp1 and temp2):

		prev1 = temp1
		next1 = temp1.nextnode

		prev2 = temp2
		next2 = temp2.nextnode

		prev1.nextnode = prev2
		prev2.nextnode = next1

		temp2 = next2
		temp1 = next1

	if temp2:
		prev2.nextnode = temp2

	return head1

lList1 = initialize.initialize_linked_list()
lList2 = initialize.initialize_linked_list_by_array([4, 5, 6, 7, 8, 9])

head1 = alternate_merge(lList1.head, lList2.head)

while (head1):
	print head1.data
	head1 = head1.nextnode