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, and only nodes itself is 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: Using the same method as above, but this time will be K reversal, each find a reversal pair, and then use the head interpolation method to reverse the list, time complexity O (n)
The main idea: to give a list, this list every k reversal.
Java source Code (330MS):
/** * Definition for singly-linked list. * public class ListNode {* int val; * ListNode Next; * listnode (int x) {val = x;}}} */public class Soluti On {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 Language Source code (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 + + source code (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 (size Of (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 source code (268MS):
# Definition for singly-linked list.# class listnode:# def __init__ (self, x): # self.val = x# Self.next = Nonec Lass 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
Leetcode Reverse Nodes in K-group (C,c++,java,python)