This file contains hidden or 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
// Function to delete node from a linked list | |
public static Node<Integer> delete(Node<Integer> data, int pos) { | |
if (head == null) { | |
return head; | |
} | |
int i = 0; | |
if (pos == 0) { | |
return head.next; | |
} | |
Node<Integer> temp = head; |
This file contains hidden or 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
// Function to insert node in a linked list | |
public static Node<Integer> insert(Node<Integer>, int data, int pos) { | |
Node<Integer> newNode = new Node<Integer>(data); | |
if (pos == 0) { | |
newNode.next = head; | |
return newNode; | |
} | |
Node<Integer> temp = head; | |
int i = 0; | |
while (temp != null && i < pos - 1) { |
This file contains hidden or 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
// Function to print a linked list | |
public static void printList(Node<Integer> head) { | |
if (head == null) { | |
return; | |
} | |
while (head != null) { | |
System.out.println(head.data); | |
head = head.next; | |
} | |
} |
This file contains hidden or 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
// 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; |
This file contains hidden or 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
Node<Integer> node1 = new Node<Integer>(10); |
This file contains hidden or 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
public class Node<T> { | |
T data; | |
Node<T> next; | |
Node(T data) { | |
this.data = data; | |
next = null; | |
} | |
} |
This file contains hidden or 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
public class Node<T> { | |
T data; | |
Node<T> next; | |
Node(T data) { | |
this.data = data; | |
next = null; | |
} | |
} |