Skip to content

Commit

Permalink
Merge pull request #138 from angha-varangaonkar/main
Browse files Browse the repository at this point in the history
Add potd_09_10_2024.java
  • Loading branch information
Gyanthakur authored Oct 9, 2024
2 parents aeb2e42 + 7a2ba9d commit f248478
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions october_2024/potd_09_10_2024.java
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;
}
}

0 comments on commit f248478

Please sign in to comment.