Java LeetCode Practice Set 3 - LinkedList
Java LeetCode Practice Set 3 - LinkedList
Page 1 of 10
Implementing a Linked List in Java using Class
class Node{
int data;
Node next;
import java.io.*;
import java.util.*;
tail.next = newNode;
// storing newnode in tail
tail = newNode;
}
Page 2 of 10
}
void displayNodes() {
Node current = head;
if (head == null) {
System.out.println("Empty");
return;
}
System.out.println("Nodes : ");
while (current != null) {
count++;
currentNode = currentNode.next;
}
return count;
}
public static void main(String[] args) {
LinkedListCreation L1 = new LinkedListCreation();
L1.addNode(1);
L1.addNode(2);
L1.addNode(3);
L1.addNode(4);
L1.displayNodes();
// Counts the nodes present in the given list
System.out.println("Total Nodes: " + L1.countNodes());
}
}
Page 3 of 10
141. Linked List Cycle
Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously
following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is
connected to. Note that pos is not passed as a parameter.
Return true if there is a cycle in the linked list. Otherwise, return false.
Example 1:
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-
indexed).
Example 2:
Output: false
Expla
nation: There is no cycle in the linked list.
Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two
lists.
Example 1:
Output: [1,1,2,3,4,4]
Example 2:
Output: []
Example 3:
Output: [0]
1st Approach
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if(list1 == null)
return list2;
if(list2 == null)
return list1;
if (l1 != null) {
handler.next = l1;
} else if (l2 != null) {
handler.next = l2;
}
return head.next;
}
}
Page 6 of 10
203. Remove Linked List Elements
Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val ==
val, and return the new head.
Example 1:
Output: [1,2,3,4,5]
Example 2:
Output: []
1st Approach
class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head==null) return head;
ListNode resultHead = new ListNode(-1);
resultHead.next=head;
ListNode curr = head, prev=resultHead;
while(curr!=null){
// ListNode temp2=temp.next;
if(curr.val == val){
prev.next=curr.next;
}
else{
prev=curr;
}
curr=curr.next;
}
return resultHead.next;
}
}
2nd Approach
public ListNode removeElements(ListNode head, int val) {
if (head == null) return null;
head.next = removeElements(head.next, val);
return head.val == val ? head.next : head;
}
Page 7 of 10
206. Reverse Linked List
Given the head of a singly linked list, reverse the list, and return the reversed list.
Example 1:
Output: [5,4,3,2,1]
class Solution {
public ListNode reverseList(ListNode head) {
// base case
if(head==null || head.next == null)
return head;
// iterate till last node
ListNode newHead = reverseList(head.next);
head.next.next = head;
head.next=null;
return newHead;
}
}
Page 8 of 10
83. Remove Duplicates from Sorted List
Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the
linked list sorted as well.
Example 1:
Output: [1,2]
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head==null || head.next==null)
return head;
head.next=deleteDuplicates(head.next);
if(head.val==head.next.val){
return head.next;
}
else{
return head;
}
}
}
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode temp = head;
while(temp!=null && temp.next!=null){
if(temp.val==temp.next.val){
temp.next=temp.next.next;
}
else
temp=temp.next;
}
return head;
}
}
Page 9 of 10
Page 10 of 10