from GeeksForGeeks.LinkedList.SinglyLinkedList import LinkedList
"""
function a revervse a linkedlist
"""


def reverse_ll(head):
    """
    reversing the pointers for every node - Single traveral - O(n) time.
    :param head
    :return:
    """
    prev = None
    current = head
    while current:
        temp = current.next
        current.next = prev
        prev = current
        current = temp
    return prev


if __name__ == '__main__':
    ll = LinkedList()
    for i in range(5, 9):
        ll.insert_at_ending(i)

    ll.print_list()

    ll.head = reverse_ll(ll.head)
    ll.print_list()
        current = head
        next = None
        prev = None
        count = 0
        while current and count < k:
            next = current.next
            current.next = prev
            prev = current
            current = next
            count += 1

        if next:
            head.next = current

        count = 0
        while current and count < k - 1:
            current = current.next
            count += 1

        if current:
            current.next = reverse_alt_knodes(current.next, k)
        return prev


if __name__ == '__main__':
    ll = LinkedList()
    for i in range(1, 10):
        ll.insert_at_ending(i)
    ll.head = reverse_alt_knodes(ll.head, 3)
    ll.print_list()
from GeeksForGeeks.LinkedList.SinglyLinkedList import LinkedList
"""
Move last element to front of a given Linked List
Write a C function that moves last element to front in a given Singly Linked List. For example, if the given Linked
List is 1->2->3->4->5, then the function should change the list to 5->1->2->3->4.
"""


def move_nth(head):
    if not head:
        return
    else:
        current = head
        prev = head
        while current.next is not None:
            prev = current
            current = current.next
        prev.next = None
        current.next = head
        head = current
        return head


if __name__ == '__main__':
    ll = LinkedList()
    for i in range(1, 10):
        ll.insert_at_ending(i)
    ll.head = move_nth(ll.head)
    ll.print_list()
示例#4
0
def delete_nodes(head):
    if not head:
        return
    else:
        new_head = reverse(head)

        current = new_head
        _max = current.data
        while current and current.next:
            if current.next.data < _max and current.next:
                current.next = current.next.next
            else:
                _max = current.data
            current = current.next

        head1 = reverse(new_head)
        return head1


if __name__ == '__main__':
    ll = LinkedList()
    ll.insert_at_ending(12)
    ll.insert_at_ending(15)
    ll.insert_at_ending(10)
    ll.insert_at_ending(11)
    ll.insert_at_ending(5)
    ll.insert_at_ending(6)
    ll.insert_at_ending(2)
    ll.insert_at_ending(3)
    ll.head = delete_nodes(ll.head)
    ll.print_list()
        while current  and count < k :
            next = current.next
            current.next = prev
            prev = current
            current = next
            count += 1

        if next :
            head.next = reverse_in_groups(next,k)
        return prev


if __name__=='__main__':
    ll = LinkedList()
    for i in range(1,3):
        ll.insert_at_ending(i)

    ll.head = reverse(ll.head)
    #ll.print_list()
    ll.head = reverse(ll.head)


    ll.head = reverse_in_groups(ll.head,1)
    ll.print_list()






示例#6
0
        return node

    current = node.next
    prev = node
    node = current

    while prev and current :
        next = current.next
        current.next = prev

        if not next or not next.next :
            prev.next = next
            break
        prev.next = next.next
        prev = next
        current = prev.next

    return node



if __name__=='__main__':
    ll = LinkedList()
    for i in range(1,10):
        ll.insert_at_ending(i)
    #ll.print_list()
    #pairwise_swap(ll.head)
    #ll.print_list()

    ll.head = _pairwise_swap(ll.head)
    ll.print_list()
示例#7
0
            current = current.next

        while cur2:
            _sum = cur2.data + carry
            carry = _sum // 10
            _sum = _sum % 10
            current.next = Node(_sum)
            cur2 = cur2.next
            current = current.next

        if carry == 1:
            current.next = Node(carry)

        return new_head


if __name__ == '__main__':
    ll1 = LinkedList()
    ll1.insert_at_ending(3)
    ll1.insert_at_ending(6)
    ll1.insert_at_ending(5)

    ll2 = LinkedList()
    ll2.insert_at_ending(2)
    ll2.insert_at_ending(4)
    ll2.insert_at_ending(8)

    ll = LinkedList()
    ll.head = add(ll1.head, ll2.head)
    ll.print_list()