LeetCode 25 Reverse Nodes in k-Group (C,C++,Java,Python)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了LeetCode 25 Reverse Nodes in k-Group (C,C++,Java,Python),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3288字,纯文字阅读大概需要5分钟。
内容图文
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:
题目大意:
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
原文:http://blog.csdn.net/runningtortoises/article/details/45647795
内容总结
以上是互联网集市为您收集整理的LeetCode 25 Reverse Nodes in k-Group (C,C++,Java,Python)全部内容,希望文章能够帮你解决LeetCode 25 Reverse Nodes in k-Group (C,C++,Java,Python)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。