LeetCode025 Reverse Nodes in k-Group

来源:互联网 发布:网络销售是怎么做的 编辑:程序博客网 时间:2024/06/04 22:47

详细见:leetcode.com/problems/reverse-nodes-in-k-group

Java Solution: github

package leetcode;/* * 25. Reverse Nodes in k-Group  QuestionEditorial Solution  My SubmissionsTotal Accepted: 67154Total Submissions: 235170Difficulty: HardGiven a linked list, reverse the nodes of a linked list k at a time and return its modified 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.For example,Given this linked list: 1->2->3->4->5For k = 2, you should return: 2->1->4->3->5For k = 3, you should return: 3->2->1->4->5 */import tools.ListNode辅助.ListNode;;public class P025_ReverseNodesInKGroup {public static void main(String[] args) {ListNode input = tools.ListNode辅助.A_一维生成器(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17});//ListNode input = tools.ListNode辅助.A_一维生成器(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});tools.ListNode辅助.B_打印链表(new Solution1().reverseKGroup(input, 2));}/* * 花了很长时间 * 代码写得非常乱,今后需要整理一下 * 可能还有先全反转,对尾部二次反转的做法,可能更快 * 1 ms * 4.84% */static class Solution1 {    public ListNode reverseKGroup(ListNode head, int k) {    if (head == null || head.next == null)    return head;    int count = 1;    ListNode ans = head, pre = null, cur = null, next_pre = null, next_cur = null, rev_cur = null, rev_pre = null, temp = null;    while (ans != null && count != k) {    ans = ans.next;    count ++;    }    cur = ans;    if (ans == null)    return head;    while (cur != null) {    if (count != k) {    count ++;    cur = cur.next;    } else {    if (pre == null) {    next_pre = head;    rev_cur = next_pre;    } else {    next_pre = pre.next;    rev_cur = next_pre;    pre.next = cur;    }    next_cur = cur.next;    rev_pre = next_cur;    while (rev_cur != next_cur) {    temp = rev_cur.next;    rev_cur.next = rev_pre;    rev_pre = rev_cur;    if (rev_cur == cur)    break;    rev_cur = temp;    }    cur = next_cur;    pre = next_pre;    count = 1;    }    }    return ans;    }}}

C Solution: github

/*    url: leetcode.com/problems/reverse-nodes-in-k-group/    12ms 7.08%*/#include <stdio.h>#include <stdlib.h>struct ListNode {    int val;    struct ListNode *next;};struct ListNode * convert_int_to_ListNode(int * arr, int n) {    struct ListNode * head = NULL;    struct ListNode * travel = NULL;    struct ListNode * temp = NULL;    int i = 0;    if (n == 0 || n < 0) return NULL;    travel = (struct ListNode *) malloc(sizeof(struct ListNode));    travel->val = *(arr + 0);    travel->next = NULL;    head = travel;    for (i = 1; i < n; i ++) {        temp = (struct ListNode *) malloc(sizeof(struct ListNode));        temp->val = *(arr + i);        temp->next = NULL;        travel->next = temp;        travel = travel->next;    }    return head;}void free_ListNode(struct ListNode * l) {    struct ListNode * temp = NULL;    while (l != NULL) {        temp = l->next;        free(l);        l = temp;    }}void print_ListNode(struct ListNode * h) {    while (h != NULL) {        printf("%d ", h->val);        h = h->next;    }    printf("\r\n");}struct ListNode* reverseKGroup(struct ListNode* head, int k) {    int i = 0;    struct ListNode * s = head, * t = NULL, * v = NULL, * pv = NULL;    struct ListNode * t1 = NULL, * t2 = NULL, * t0 = NULL;    struct ListNode * answer = head;    if (k < 2 || head == NULL) return answer;    while (s != NULL) {        t = s;        for (i = 1; i < k; i ++) {            if (t == NULL) break;            t = t->next;        }        if (pv != NULL) {            pv->next = t == NULL ? s : t;        }        pv = s;        if (t == NULL) break;        if (answer == head) answer = t;        v = t->next;        t0 = s;        t1 = t0->next;        t2 = t1 != NULL ? t1->next : NULL;        while (1) {            t1->next = t0;            if (t1 == t) break;            t0 = t1;            t1 = t2;            t2 = t2 == NULL ? NULL : t2->next;        }        s = v;    }    if (s == NULL)        pv->next = NULL;    return answer;}int main() {    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};    int k = 11;    struct ListNode * head = convert_int_to_ListNode(a, 10);    struct ListNode * answer = reverseKGroup(head, k);    print_ListNode(answer);    free_ListNode(answer);}


Python Solution: github
#coding=utf-8'''    url: leetcode.com/problems/reverse-nodes-in-k-group/    @author:     zxwtry    @email:      zxwtry@qq.com    @date:       2017年3月31日    @details:    Solution: 78ms 60.31%'''from leetcode.Utils import *class Solution(object):    def reverseKGroup(self, head, k):        """        :type head: ListNode        :type k: int        :rtype: ListNode        """        if k < 2: return head        ans, t0, t3 = head, None, head        while True:            t1, t2 = t3, t3            for i in range(k - 1):                t2 = None if t2 == None else t2.next            if t2 == None:                if t0 != None: t0.next = t1                break            if t0 == None: ans = t2            else: t0.next = t2            r0, r1 = t0, t1            t3, t0 = t2.next, t1            while r1 != t3:                r2 = r1.next                r1.next = r0                r0, r1 = r1, r2        return ans                        if __name__ == "__main__":    head = convertArrayToListNode([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])    k = 4    sol = Solution()    printListNode(sol.reverseKGroup(head, k))




0 0
原创粉丝点击