TypechoJoeTheme

IT技术分享

统计

[LeetCode 25] Reverse Nodes in k-Group [C, Java] [Runtime : 6 MS]

2017-06-09
/
0 评论
/
569 阅读
/
正在检测是否收录...
06/09

1. Description

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

You may not alter the values in the nodes, only nodes itself may be changed.

Only constant memory is allowed.

2. Example

Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5

3. Code

struct ListNode * reverseKNode(struct ListNode * start, int k)
{
    struct ListNode * end = start->next;
    int i = k;
    while(i-- > 0 && end != NULL )
    {
        end = end->next;
    }

    if(i >= 0)
    {
        return end;
    }

    struct ListNode *current = start->next-> next, *preNode = start-> next;
    while(--k > 0)
    {
        struct ListNode *tmp = current->next;
        current->next = preNode;
        preNode = current;
        current = tmp;
    }
    end = start->next;
    end->next = current;
    start->next = preNode;

    return end;
}

struct ListNode * reverseKGroup(struct ListNode* head, int k)
{
    if(head == NULL || k == 1 )
    {
        return head;
    }

    struct ListNode * dummy = (struct ListNode *) malloc(sizeof(struct ListNode));
    dummy->next = head;

    struct ListNode * start = dummy;
    while(start != NULL)
    {
        start = reverseKNode(start, k);
    }
    return dummy->next;
}
public ListNode reverseKGroup(ListNode head, int k) {

    if(k == 1 || head == null){
        return head;
    }

    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode start = dummy;
    while(start != null){
        start = reverseKNode(start, k);
    }
    return dummy.next;
}

private ListNode reverseKNode(ListNode start, int k){
    ListNode end = start;

    int i = k;
    while(i--  > 0 && end.next != null){
        end = end.next;
    }
    if(i >= 0){
        return null;
    }

    ListNode current = start.next.next, preNode = start.next;

    while(--k > 0){
        ListNode tmp = current.next;
        current.next = preNode;
        preNode = current;
        current = tmp;
    }
    end = start.next;
    end.next = current;
    start.next = preNode;

    return end;
}

4.Test

#include<stdio.h>
#include <stdlib.h>

struct ListNode
{
    int val;
    struct ListNode * next;
};

struct ListNode * reverseKNode(struct ListNode * start, int k)
{
    struct ListNode * end = start->next;
    int i = k;
    while(i-- > 0 && end != NULL )
    {
        end = end->next;
    }

    if(i >= 0)
    {
        return end;
    }

    struct ListNode *current = start->next-> next, *preNode = start-> next;
    while(--k > 0)
    {
        struct ListNode *tmp = current->next;
        current->next = preNode;
        preNode = current;
        current = tmp;
    }
    end = start->next;
    end->next = current;
    start->next = preNode;

    return end;
}

struct ListNode * reverseKGroup(struct ListNode* head, int k)
{
    if(head == NULL || k == 1 )
    {
        return head;
    }

    struct ListNode * dummy = (struct ListNode *) malloc(sizeof(struct ListNode));
    dummy->next = head;

    struct ListNode * start = dummy;
    while(start != NULL)
    {
        start = reverseKNode(start, k);
    }
    return dummy->next;
}

int main()
{
    struct ListNode * ln = (struct ListNode *) malloc(3 *sizeof(struct ListNode));
    ln->val = 1;
    (ln + 1)->val = 2;
    (ln + 2)->val = 3;
    ln->next = ln + 1;
    (ln + 1)->next = ln + 2;
    (ln + 2)->next = NULL;
    struct ListNode * result = reverseKGroup(ln, 3);

    while (result != NULL)
    {
        printf("%d", result->val);
        result = result->next;
    }

    system("pause");
    return 0;
}
import org.junit.Test;

class ListNode {
     int val;
     ListNode next;
     ListNode(int x) { val = x; }
 }

public class LeetCode0025 {
    public ListNode reverseKGroup(ListNode head, int k) {

        if(k == 1 || head == null){
            return head;
        }

        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode start = dummy;
        while(start != null){
            start = reverseKNode(start, k);
        }
        return dummy.next;
    }

    private ListNode reverseKNode(ListNode start, int k){
        ListNode end = start;

        int i = k;
        while(i--  > 0 && end.next != null){
            end = end.next;
        }
        if(i >= 0){
            return null;
        }

        ListNode current = start.next.next, preNode = start.next;

        while(--k > 0){
            ListNode tmp = current.next;
            current.next = preNode;
            preNode = current;
            current = tmp;
        }
        end = start.next;
        end.next = current;
        start.next = preNode;

        return end;
    }

    @Test
    public void test(){
        ListNode ln1 = new ListNode(1);
        ListNode ln2 = new ListNode(2);
        ListNode ln3 = new ListNode(3);
        ln1.next = ln2;
        ln2.next = ln3;
        ListNode result = reverseKGroup(ln1, 3);
        while(result != null){
            System.out.print(result.val);
            result = result.next;
        }
    }
}

5. Submission Details

6. Runtime Distribution

Linked
朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/353/(转载时请注明本文出处及文章链接)