Exemple #1
0
        :type head: ListNode
        :rtype: ListNode
        """
        
        node = head;
        result = node;
        previous = None;
        while node != None:
            nextNode = node.next;
            
            if nextNode != None:
                node.next = nextNode.next;
                nextNode.next = node;
                if node == head:
                    result = nextNode;
                if previous != None:
                    previous.next = nextNode;
                
            previous = node;
            node = node.next;
        
        return result;

list = [1,2,3,4,5];
controller = ListNodeController();
controller.addAll(list);
listString = controller.toString();
result = Solution().swapPairs(controller.getHead());
resultController = ListNodeController(result);

print '%s = %s' % (listString , resultController.toString());
Exemple #2
0
            while node != None and i< k:
                previous = node;
                node = node.next;                
                i+= 1;
            if i == k:
                previous.next = None;
                reverseList = self.reverseList(currentListHead);
                if currentListHead == head:
                    result = reverseList;
                currentListHead.next = node;
                
                if previousListTail != None:
                    previousListTail.next = reverseList;
                previousListTail = currentListHead;
            else:
                break;
        
        return result; 
                
#list = [1,2,3,4,5,6,7,8,9];
list =[1,2,3];
k = 1;
controller = ListNodeController();
controller.addAll(list);
listString = controller.toString();


result = Solution().reverseKGroup(controller.getHead(), k);
resultController = ListNodeController(result);

print '%s = %s' % (listString , resultController.toString());