-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b589d7
commit 15b0ba0
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
//{ Driver Code Starts | ||
/*package whatever //do not write package name here */ | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class Node { | ||
int data; | ||
Node next; | ||
|
||
public Node (int data){ | ||
this.data = data; | ||
this.next = null; | ||
} | ||
} | ||
|
||
class GFG { | ||
static void printList(Node node) | ||
{ | ||
while (node != null) | ||
{ | ||
System.out.print(node.data + " "); | ||
node = node.next; | ||
} | ||
System.out.println(); | ||
} | ||
public static void main (String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
while(t-- > 0){ | ||
int n = sc.nextInt(); | ||
|
||
Node head = new Node(sc.nextInt()); | ||
Node tail = head; | ||
|
||
for(int i=1; i<n; i++){ | ||
tail.next = new Node(sc.nextInt()); | ||
tail = tail.next; | ||
} | ||
Solution obj = new Solution(); | ||
head = obj.sort(head); | ||
printList(head); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
|
||
/* | ||
class Node { | ||
int data; | ||
Node next; | ||
public Node (int data){ | ||
this.data = data; | ||
this.next = null; | ||
} | ||
} | ||
*/ | ||
|
||
class Solution | ||
{ | ||
|
||
public Node sort(Node head) | ||
{ | ||
//your code here, return the head of the sorted list | ||
if(head==null||head.next==null) | ||
return head; | ||
|
||
Node t=head; | ||
Node n1=new Node(-1); | ||
Node n2=new Node(-1); | ||
Node p1=n1,p2=n2; | ||
|
||
while(t!=null&&t.next!=null) | ||
{ | ||
p1.next=t; | ||
p1=t; | ||
p2.next=t.next; | ||
p2=t.next; | ||
|
||
if(t.next!=null) | ||
t=t.next.next; | ||
else | ||
break; | ||
} | ||
|
||
Node nh=n2.next; | ||
n2.next=null; | ||
p1.next=rev(nh); | ||
|
||
return n1.next; | ||
} | ||
|
||
|
||
public static Node rev(Node h) | ||
{ | ||
Node pr=null; | ||
while(h!=null) | ||
{ | ||
Node n=h.next; | ||
h.next=pr; | ||
pr=h; | ||
h=n; | ||
} | ||
return pr; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution | ||
{ | ||
public int[] productExceptSelf(int[] nums) | ||
{ | ||
int n = nums.length; | ||
int[] ans = new int[n]; | ||
ans[0] = 1; | ||
|
||
for (int i = 1; i < n; ++i) | ||
ans[i] = ans[i - 1] * nums[i - 1]; | ||
|
||
int suffix = 1; | ||
for (int i = n - 1; i >= 0; --i) | ||
{ | ||
ans[i] *= suffix; | ||
suffix *= nums[i]; | ||
} | ||
|
||
return ans; | ||
} | ||
} |