LeetCode 25 Reverse Nodes in k-Group (C,C++,Java,Python)
来源:互联网 发布:淘宝代练那家店好 编辑:程序博客网 时间:2024/05/25 23:25
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 = Noneclass 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
0 0
- LeetCode 25 Reverse Nodes in k-Group (C,C++,Java,Python)
- [LeetCode] 025. Reverse Nodes in k-Group (Hard) (C++/Java)
- [leetcode-25]Reverse Nodes in k-Group(C)
- leetcode 25:Reverse Nodes in k-Group(C)
- LeetCode-25-Reverse Nodes in k-Group(C语言实现)
- LeetCode 之 Reverse Nodes in k-Group — C 实现
- [LeetCode练习题-C语言] 25. Reverse Nodes in k-Group
- 【LeetCode】 25. Reverse Nodes in k-Group C语言
- 【LeetCode算法练习(C++)】Reverse Nodes in k-Group
- 【Leetcode】【python】Reverse Nodes in k-Group
- [Leetcode] Reverse Nodes in k-Group (Java)
- [LeetCode][Java] Reverse Nodes in k-Group
- LeetCode : Reverse Nodes in k-Group [java]
- Reverse Nodes in k-Group leetcode java
- LeetCode(25)Reverse Nodes in K-Group
- [leetcode 25]Reverse Nodes in k-Group
- [leetcode #25]Reverse Nodes in k-Group
- leetcode 25 Reverse Nodes in k-Group
- Mac下反编译AndroidAPK
- C++中STL STD::list使用说明
- 蓝桥杯 VIP 阶乘计算
- Leetcode Binary Tree Postorder Traversal 二叉树后序遍历
- 前景检测算法 (GMM)
- LeetCode 25 Reverse Nodes in k-Group (C,C++,Java,Python)
- paragraph2vec工作受质疑
- Oracle中dual表的用途介绍
- Unity 大牛的开发经验分享
- matlab分布式计算平台(多机协作)搭建
- STL通用函数与list用法详解
- Java传递
- Web 前端攻防(2014版)
- Mysql数据库中文乱码