-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from angha-varangaonkar/main
Add potd_09_10_2024.java
- Loading branch information
Showing
1 changed file
with
83 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,83 @@ | ||
|
||
|
||
// User function Template for Java | ||
|
||
/*class Node | ||
class Node | ||
{ | ||
int data; | ||
Node right,down; | ||
Node(int data){ | ||
this.data = data; | ||
right = null; | ||
down = null; | ||
} | ||
} | ||
*/ | ||
/*Function should return the head of the 2D LL.*/ | ||
class Solution { | ||
static Node head; | ||
static Node construct(int arr[][]) { | ||
// Add your code here. | ||
int n=arr.length; | ||
|
||
if(n==1) | ||
{ | ||
Node newnode =new Node(arr[0][0]); | ||
head=newnode; | ||
return head; | ||
} | ||
|
||
Node newnode =new Node(arr[0][0]); | ||
head=newnode; | ||
|
||
Node currnode=newnode; | ||
Node firstnode=head; | ||
Node secondnode=head; | ||
|
||
for(int i=0;i<n;i++) | ||
{ | ||
|
||
if(i>0) | ||
{ | ||
|
||
Node newnode3=new Node(arr[i][0]); | ||
firstnode.down=newnode3; | ||
|
||
currnode=newnode3; | ||
firstnode=firstnode.down; | ||
|
||
} | ||
|
||
|
||
for(int j=1;j<n;j++) | ||
{ | ||
Node newnode2=new Node(arr[i][j]); | ||
currnode.right=newnode2; | ||
currnode=currnode.right; | ||
} | ||
|
||
} | ||
|
||
firstnode = head; | ||
while (firstnode.down != null) | ||
{ | ||
Node row1 = firstnode; | ||
Node row2 = firstnode.down; | ||
|
||
while (row1 != null && row2 != null) | ||
{ | ||
row1.down = row2; | ||
row1 = row1.right; | ||
row2 = row2.right; | ||
} | ||
|
||
firstnode = firstnode.down; | ||
} | ||
|
||
|
||
return head; | ||
} | ||
} |