A 2D array in Java is an array of arrays. It can be visualized as a table with rows and columns. For example, a int[][]
2D array can be declared and initialized as follows:
int[][] twoDArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
A linked list is a data structure where each node contains a data element and a reference (or link) to the next node in the list. In Java, the java.util.LinkedList
class provides an implementation of the linked list data structure. However, for the purpose of this conversion, we will create our own simple linked list node class.
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
this.next = null;
}
}
// Define the ListNode class
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
this.next = null;
}
}
public class TwoDArrayToLinkedList {
public static ListNode convert2DArrayToLinkedList(int[][] twoDArray) {
if (twoDArray == null || twoDArray.length == 0) {
return null;
}
// Create a dummy node to simplify the code
ListNode dummy = new ListNode(0);
ListNode current = dummy;
// Traverse the 2D array
for (int i = 0; i < twoDArray.length; i++) {
for (int j = 0; j < twoDArray[i].length; j++) {
// Create a new node for each element in the 2D array
ListNode newNode = new ListNode(twoDArray[i][j]);
// Connect the new node to the current list
current.next = newNode;
// Move the current pointer to the new node
current = newNode;
}
}
// Return the head of the linked list (skipping the dummy node)
return dummy.next;
}
public static void printLinkedList(ListNode head) {
ListNode current = head;
while (current != null) {
System.out.print(current.val + " -> ");
current = current.next;
}
System.out.println("null");
}
public static void main(String[] args) {
int[][] twoDArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
ListNode head = convert2DArrayToLinkedList(twoDArray);
printLinkedList(head);
}
}
In this code, we first define a ListNode
class to represent the nodes in the linked list. The convert2DArrayToLinkedList
method takes a 2D array as input and converts it into a linked list. We use a dummy node to simplify the code and avoid dealing with special cases for the head of the linked list. Finally, we print the linked list to verify the conversion.
NullPointerException
.next
pointers are set correctly. A wrong link can lead to an incorrect or broken linked list.NullPointerException
.Converting a 2D array to a linked list in Java can be a useful operation in various scenarios. By understanding the core concepts, typical usage scenarios, common pitfalls, and best practices, you can implement this conversion effectively and efficiently. The code example provided in this blog post demonstrates a simple and straightforward way to perform this conversion.
java.util.LinkedList
class for this conversion?Yes, you can use the java.util.LinkedList
class. However, the example in this blog post creates a simple custom linked list node class for better understanding of the underlying concepts.
The time complexity of converting a 2D array to a linked list is $O(m * n)$, where $m$ is the number of rows and $n$ is the number of columns in the 2D array.
Yes, you can convert a linked list back to a 2D array. The process involves traversing the linked list and populating the 2D array accordingly.