Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add potd_09_10_2024.java #138

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading