ItsRunTym
DSA Coding Sheet
Top 17 LINKEDLIST questions
LinkedList Implementation :
LinkedList Implementation - 2:
Doubly LinkedList Implementation :
S. No. Problem Name Difficulty Link Sol
1 Linked List Cycle Easy
2 Add Two Numbers Medium
3 Merge Two Sorted Lists Easy
Copy List with Random
4 Medium
Pointer
5 Reverse Linked List II Medium
6 Reverse Nodes in k-Group Hard
Remove Nth Node From End
7 Medium
of List
Remove Duplicates from
8 Medium
Sorted List II
9 Rotate List Medium
S. No. Problem Name Difficulty Link Sol
10 Partition List Medium
11 LRU Cache Medium
Find intersection point of Y
12 Medium
LinkedList
13 Detect a cycle in Linked List Medium
Reverse a LinkedList in
14 Hard
groups of size k
Check if a LinkedList is
15 Medium
palindrome or not
Find the starting point of the
16 Medium
Loop of LinkedList
17 Flattening of a LinkedList Hard
1 Linked List Cycle Easy
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode tmp = head;
ListNode tmp1 = head;
while(tmp1!=null && tmp1.next!=null){
tmp=tmp.next;
tmp1=tmp1.next.next;
if(tmp == tmp1){
return true;
return false;
2 Add Two Numbers Medium
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode
l2) {
ListNode ll = new ListNode();
ListNode last = ll;
int size=0;
int carry =0;
while(l1!=null || l2!=null || carry!=0){
int sum = carry;
if(l2!=null){
sum+=l2.val;
l2=l2.next;
if(l1!=null){
sum+=l1.val;
l1=l1.next;
carry= sum/10;
int currDig = sum%10;
ListNode ll2 = new ListNode();
ll2.val = currDig;
if(size==0){
ll.val = currDig;
else{
if(size==1){
ll.next = ll2;
}else{
last.next = ll2;
last= ll2;
size++;
last.next=null;
return ll;
3 Merge Two Sorted Lists Easy
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1==null || l2==null){
return l1!=null?l1:l2;
ListNode result = new ListNode();
ListNode pre = result;
while(l1!=null && l2!=null){
if(l1.val<l2.val){
pre.next=l1;
l1=l1.next;
pre=pre.next;
}else{
pre.next=l2;
l2=l2.next;
pre=pre.next;
pre.next = l1!=null?l1:l2;
return result.next;
4 Copy List with Random Pointer Medium
public class Solution {
public Node copyRandomList(Node head) {
if (head == null) return null;
HashMap<Node, Node> oldToNew = new
HashMap<>();
Node curr = head;
while (curr != null) {
oldToNew.put(curr, new Node(curr.val));
curr = curr.next;
curr = head;
while (curr != null) {
oldToNew.get(curr).next = oldToNew.get(curr.next);
oldToNew.get(curr).random =
oldToNew.get(curr.random);
curr = curr.next;
return oldToNew.get(head);
5 Reverse Linked List II Medium
class Solution {
public ListNode reverseBetween(ListNode head, int left,
int right) {
if(head==null || left==right){
return head;
ListNode dumy = new ListNode(0);
dumy.next=head;
ListNode prev = dumy;
for(int i=0;i<left-1;i++){
prev=prev.next;
ListNode curr = prev.next;
for(int i=0;i<right-left;i++){
ListNode tmp = curr.next;
curr.next=tmp.next;
tmp.next=prev.next;
prev.next=tmp;
}
return dumy.next;
6 Reverse Nodes in k-Group Hard
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode dumy = new ListNode(0);
dumy.next=head;
ListNode prev = dumy;
ListNode curr = prev.next;
int size = 0;
while(curr!=null){
curr= curr.next;
size++;
int grp = size/k;
int z =0;
curr=prev.next;
while(z<grp){
int i=0;
while(i<k-1){
ListNode tmp = curr.next;
curr.next= tmp.next;
tmp.next = prev.next;
prev.next= tmp;
i++;
}
prev=curr;
curr=curr.next;
z++;
return dumy.next;
7 Remove Nth Node From End of List Medium
class Solution {
public ListNode removeNthFromEnd(ListNode head, int
n) {
int size =0;
ListNode dumy = new ListNode(0);
dumy.next=head;
ListNode curr = dumy.next;
while(curr!=null){
curr=curr.next;
size++;
curr=dumy;
for(int i=0;i<size-n;i++){
curr=curr.next;
curr.next = curr.next.next;
return dumy.next;
8 Remove Duplicates from Sorted List II Medium
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if( head == null || head.next==null){
return head;
ListNode prev = null;
ListNode rem = null;
ListNode curr = head;
ListNode next = head.next;
ListNode result = null;
while(next!=null){
if(curr.val != next.val){
if(prev==null){
result = curr;
prev=curr;
}else{
rem =curr;
while(rem.val==next.val){
curr=curr.next;
next=next.next;
if(next==null){
if(result!=null){
prev.next=null;
return result;
if(prev!=null){
prev.next = next;
curr.next = null;
curr=next;
next= next.next;
if(next==null){
if(result==null){
result = curr;
return result;
9 Rotate List Medium
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if( head==null){
return head;
int size = 0;
ListNode curr = head;
while(curr!=null){
curr=curr.next;
size++;
}
k = k%size;=
if( k==0){
return head;
curr= head;
ListNode newHead = curr.next;
for(int i=0;i<size-k-1;i++){
curr=curr.next;
newHead = curr.next;
curr.next = null;
curr = newHead;
while(curr.next!=null){
curr=curr.next;
curr.next=head;
return newHead;
10 Partition List Medium
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode slist = new ListNode(0);
ListNode blist = new ListNode(0);
ListNode small = slist;
ListNode big = blist;
while (head != null) {
if (head.val < x) {
small.next = head;
small = small.next;
} else {
big.next = head;
big = big.next;
head = head.next;
small.next = blist.next;
big.next = null;
return slist.next;
11 LRU Cache Medium
class LRUCache {
class Node {
int key;
int val;
Node prev;
Node next;
Node(int key, int val) {
this.key = key;
this.val = val;
}
Node head = new Node(-1, -1);
Node tail = new Node(-1, -1);
int cap;
HashMap<Integer, Node> m = new HashMap<>();
public LRUCache(int capacity) {
cap = capacity;
head.next = tail;
tail.prev = head;
private void addNode(Node newnode) {
Node temp = head.next;
newnode.next = temp;
newnode.prev = head;
head.next = newnode;
temp.prev = newnode;
private void deleteNode(Node delnode) {
Node prevv = delnode.prev;
Node nextt = delnode.next;
prevv.next = nextt;
nextt.prev = prevv;
public int get(int key) {
if (m.containsKey(key)) {
Node resNode = m.get(key);
int ans = resNode.val;
m.remove(key);
deleteNode(resNode);
addNode(resNode);
m.put(key, head.next);
return ans;
return -1;
public void put(int key, int value) {
if (m.containsKey(key)) {
Node curr = m.get(key);
m.remove(key);
deleteNode(curr);
if (m.size() == cap) {
m.remove(tail.prev.key);
deleteNode(tail.prev);
addNode(new Node(key, value));
m.put(key, head.next);
}
12 Find intersection point of Y LinkedList Medium
public class Solution {
public ListNode getIntersectionNode(ListNode headA,
ListNode headB) {
if(headA==null || headB==null){
return null;
ListNode temp1 =headA;
ListNode temp2 = headB;
while(temp1!=temp2){
temp1= temp1==null?headB:temp1.next;
temp2= temp2==null?headA:temp2.next;
if(temp1==temp2) return temp1;
return temp1;
13 Detect a cycle in Linked List Medium
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode tmp = head;
ListNode tmp1 = head;
while(tmp1!=null && tmp1.next!=null){
tmp=tmp.next;
tmp1=tmp1.next.next;
if(tmp == tmp1){
return true;
}
}
return false;
14 Reverse a LinkedList in groups of size k Hard
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode dumy = new ListNode(0);
dumy.next=head;
ListNode prev = dumy;
ListNode curr = prev.next;
int size = 0;
while(curr!=null){
curr= curr.next;
size++;
int grp = size/k;
int z =0;
curr=prev.next;
while(z<grp){
int i=0;
while(i<k-1){
ListNode tmp = curr.next;
curr.next= tmp.next;
tmp.next = prev.next;
prev.next= tmp;
i++;
}
prev=curr;
curr=curr.next;
z++;
return dumy.next;
15 Check if a LinkedList is palindrome or not Medium
class Solution {
public boolean isPalindrome(ListNode head) {
ListNode temp=head;
ArrayList<Integer> newlist = new
ArrayList<Integer>();
boolean f=true;
int count=0;
while(temp!=null){
count++;
newlist.add(temp.val);
temp=temp.next;
for(int i=0;i<count/2;i++){
int x=newlist.get(i);
int y=newlist.get(count-i-1);
if(x==y){
continue;
else{
f=false;
break;
return f;
16 Find the starting point of the Loop of LinkedList Medium
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while(fast!=null && fast.next!=null){
slow= slow.next;
fast=fast.next.next;
if(slow==fast){
slow=head;
while(slow!=fast){
slow=slow.next;
fast=fast.next;
return slow;
return null;
17 Flattening of a LinkedList Hard
class Solution {
// Function to flatten a linked list
Node merge(Node list1, Node list2){
Node dummy = new Node(0);
Node temp = dummy;
while(list1!=null && list2!=null){
if(list1.data < list2.data){
temp.bottom = list1;
temp = list1;
list1 = list1.bottom;
else{
temp.bottom = list2;
temp = list2;
list2 = list2.bottom;
temp.next = null;
if(list1!=null){
temp.bottom = list1;
temp.bottom.next = null;
else{
temp.bottom = list2;
temp.bottom.next = null;
return dummy.bottom;
}
Node flatten(Node head) {
if(head==null || head.next==null){
return head;
Node newHead = flatten(head.next);
head = merge(head, newHead);
return head;