LeetCode 25 Reverse Nodes in k-Group (C,C++,Java,Python)

Problem:

Given 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->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

Solution:

采用和上题基本类似的方法,只不过这次是将k个反转,每次找到一个反转对,然后采用头插法将链表反转,时间复杂度O(n)

题目大意:

给一个链表,将这个链表每K个反转。

Java源代码(330ms):

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode s,p=new ListNode(0);
        p.next=head;head=p;s=p;
        int len=k;
        while(k-->0 && s!=null)s=s.next;
        while(s!=null){
            ListNode tmp,l=p.next,flag=s.next,tail=p.next;
            p.next=null;
            while(l!=flag){
                tmp=p.next;
                p.next=l;
                l=l.next;
                p.next.next=tmp;
            }
            tail.next=l;
            p=tail;
            s=tail;
            k=len;
            while(k-->0 && s!=null)s=s.next;
        }
        return head.next;
    }
}

C语言源代码(8ms):

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
void reverse(struct ListNode** p,struct ListNode **s){
	struct ListNode *l=(*p)->next,*tmp,*tail=(*p)->next,*flag=(*s)->next;
    (*p)->next=NULL;
    while(l!=flag){
        tmp=(*p)->next;
        (*p)->next=l;
        l=l->next;
        (*p)->next->next=tmp;
    }
    tail->next=l;
    *p=tail;
    *s=tail;
}
struct ListNode* reverseKGroup(struct ListNode* head, int k) {
    struct ListNode *s,*p=(struct ListNode*)malloc(sizeof(struct ListNode));
    int len=k;
    p->next=head;
    head=p;s=p;
    while(k-- && s!=NULL)s=s->next;
    while(s!=NULL){
        reverse(&p,&s);
        k=len;
        while(k-- && s!=NULL)s=s->next;
    }
    return head->next;
}

C++源代码(30ms):

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode *s,*p=(ListNode*)malloc(sizeof(ListNode));
        int len=k;
        p->next=head;head=p;s=p;
        while(k-- && s!=NULL)s=s->next;
        while(s!=NULL){
            reverse(p,s);
            k=len;
            while(k-- && s!=NULL)s=s->next;
        }
        return head->next;
    }
private:
    void reverse(ListNode* &p,ListNode* &s){
        ListNode *tmp,*tail=p->next,*flag=s->next,*l=p->next;
        p->next=NULL;
        while(l!=flag){
            tmp=p->next;
            p->next=l;
            l=l->next;
            p->next->next=tmp;
        }
        tail->next=l;
        p=tail;
        s=tail;
    }
};

Python源代码(268ms):

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @param {ListNode} head
    # @param {integer} k
    # @return {ListNode}
    def reverseKGroup(self, head, k):
        p=ListNode(0)
        p.next=head;head=p;s=p
        len=k
        while k>0 and s!=None:k-=1;s=s.next
        while s!=None:
            flag=s.next;tail=p.next;l=p.next
            while l!=flag:
                tmp=p.next
                p.next=l
                l=l.next
                p.next.next=tmp
            tail.next=l
            p=tail;s=tail
            k=len
            while k>0 and s!=None:k-=1;s=s.next
        return head.next
时间: 2024-12-19 22:02:07

LeetCode 25 Reverse Nodes in k-Group (C,C++,Java,Python)的相关文章

(Java) LeetCode 25. Reverse Nodes in k-Group —— k个一组翻转链表

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

[LeetCode] 25. Reverse Nodes in k-Group 每k个一组翻转链表

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

leetCode 25.Reverse Nodes in k-Group (以k个节点为一组反转链表) 解题思路和方法

Reverse Nodes in k-Group Given 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 valu

LeetCode 25 Reverse Nodes in k-Group Add to List 划分list

题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description Problem :将一个有序list划分为k个组,并且每个组的元素逆置 链表操作 :递归算法 每次寻找到该组的尾部,然后进行逆置操作,返回头部,这样每次递归操作之后能够进行下一次的逆置操作. 链表操作画图比较形象!!!! 对于递归算法:找到共同点,找到程序退出点,注意特殊情况 本题中,共同点为每组为k个节点,并且每组进行的操作均为逆置操作.

LeetCode 25 Reverse Nodes in k-Group K个一组反转节点

题目: Given 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

[LeetCode]25. Reverse Nodes in k-Group k个一组翻转链表

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

leetcode 25: Reverse Nodes in k-Group

Given 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 nod

Java [leetcode 25]Reverse Nodes in k-Group

题目描述: Given 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, on

[LeetCode] 25. Reverse Nodes in k-Group ☆☆☆

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