Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Reverse a Linked List in groups of given size k
//'main' method must be in a class 'Rextester'. //openjdk version '11.0.5' import java.util.*; import java.lang.*; class Rextester { public static class Node { // public Node head; public int data; public Node next; public Node(int data) { this.data=data; this.next=null; } } public static Node reverse(Node head, int x, int dup) { if (head == null) return null; Node prev=null; Node next=null; Node curr=head; dup=dup-x; //System.out.println(dup); int c=0; while( dup >= x && c<x && curr!=null) { next=curr.next; curr.next=prev; prev=curr; curr=next; c++; } /* while(curr!=null) { System.out.println(curr.data); curr=curr.next; } */ if(next!=null) head.next=reverse(next,x,dup); return prev; } public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t>0) { t--; int n=sc.nextInt(); int x=sc.nextInt(); Node head=null; int duplicate=n; // head.next=null; while(n>0) { //System.out.println("1"); n--; int tmp=sc.nextInt(); Node ptr= new Node(tmp); ptr.next=null; // ptr=head; // System.out.println(ptr.data); if(head == null) { head=ptr; // System.out.println(head.data); } else { Node tmpp=head; while( tmpp.next !=null) tmpp=tmpp.next; tmpp.next=ptr; } } /* while(head!=null) { System.out.println(head.data); head=head.next; } */ Node head2=reverse(head,x,duplicate); while(head2!=null) { System.out.println(head2.data); head2=head2.next; } } } }
run
|
edit
|
history
|
help
0
binary square root
Random number
Add Two Numbers saved in linked lists
test1
exp5
JAVA # Klavyeden girilen cümlede kaç adet "a" harfi var
Sieve of Eratosthenes Revisited
test1
exp5
Complex Number implementation using oops java