Skip to content

Instantly share code, notes, and snippets.

@vin18
Created July 10, 2020 11:55
Show Gist options
  • Save vin18/f009153732ef31ba9c4838713fa57b2d to your computer and use it in GitHub Desktop.
Save vin18/f009153732ef31ba9c4838713fa57b2d to your computer and use it in GitHub Desktop.
// Function to take input of a linked list
public static Node<Integer> takeInput() {
Scanner s = new Scanner(System.in);
Node<Integer> head = null, tail = null;
int data = s.nextInt();
while (data != -1) {
Node<Integer> newNode = new Node<Integer>(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
data = s.nextInt();
}
s.close();
return head;
}
// Time Complexity: O(N)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment