SlideShare a Scribd company logo
TOP
INTERVIEW
QUESTIONS
EASYCOLLECTIONS
LINKEDLIST
Sunil Yadav
Linked List problems are relatively easy to master. Do
not forget the Two-pointer technique, which not only
applicable to Array problems but also Linked List
problems as well.
Another technique to greatly simplify coding in linked
list problems is the dummy node trick.
We recommend: Reverse Linked List, Merge Two
Sorted Lists and Linked List Cycle.
For additional challenge, solve these problems
recursively: Reverse Linked List, Palindrome Linked List
and Merge Two Sorted Lists.
IntroductION
Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only
access to that node.
Given linked list -- head = [4,5,1,9], which looks like following:
Example 1:
Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You are given the second node with value 5, the
linked list should become 4 -> 1 -> 9 after calling your
function.
Example 2:
Input: head = [4,5,1,9], node = 1
Output: [4,5,9]
Explanation: You are given the third node with value 1, the
linked list should become 4 -> 5 -> 9 after calling your
function.
Note:
• The linked list will have at least two elements.
• All of the nodes' values will be unique.
• The given node will not be the tail and it will always be a valid node of the
linked list.
• Do not return anything from your function.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
ListNode Prev = null;
while(node.next!=null){
node.val = node.next.val;
Prev = node;
node = node.next;
}
if(Prev!=null)
Prev.next = null;
}
}
Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head.
Example:
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list
becomes 1->2->3->5.
Note:
Given n will always be valid.
Follow up:
Could you do this in one pass?
•
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode Prev = null;
ListNode fN = head;
ListNode slow = head;
int counter = 1;
while(head!=null){
if(counter>n){
Prev = slow;
slow = slow.next;
}
counter++;
head = head.next;
}
if(Prev!=null){
if(slow!=null)
Prev.next = slow.next;
else
Prev.next = null;
}
if(Prev == null){
return fN.next;
}
return fN;
}
}
 Reverse Linked List
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement
both?
•
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode root = head;
ListNode Prev = null;
while(head!=null){
ListNode nextNode = head.next;
head.next = Prev;
Prev = head;
head = nextNode;
}
return Prev;
}
}
Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new sorted list. The new list should
be made by splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode x = l1;
ListNode y = l2;
ListNode result = new ListNode(-1);
ListNode fR = result;
while(x!=null && y!=null){
if(x.val <= y.val){
result.next = new ListNode(x.val);
x = x.next;
}else{
result.next = new ListNode(y.val);
y = y.next;
}
result = result.next;
}
if(x!=null){
result.next = x;
result = result.next;
}
if(y!=null){
result.next = y;
result = result.next;
}
return fR.next;
}
Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.
Example 1:
Input: 1->2
Output: false
Example 2:
Input: 1->2->2->1
Output: true
Follow up:

Could you do it in O(n) time and O(1) space?
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while(fast!=null && fast.next!=null){
slow = slow.next;
fast = fast.next.next;
}
Stack<ListNode> stack = new Stack<>();
// Slow is at Middle Position
while(slow!=null){
stack.push(slow);
slow = slow.next;
}
while(!stack.isEmpty()){
if(head.val!=stack.pop().val){
return false;
}
head = head.next;
}
return true;
}
}
Linked List Cycle
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer pos which
represents the position (0-indexed) in the linked list where tail connects to.
If pos is -1, then there is no cycle in the linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail
connects to the second node.
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null){
return false;
}
ListNode slow = head;
ListNode fast = head.next;
while(true){
if(slow==null || fast==null){
return false;
}
if(slow==fast){
return true;
}
slow = slow.next;
if(fast.next==null){
return false;
}
fast = fast.next.next;
}
}
}

More Related Content

What's hot (20)

7 functions
7   functions7   functions
7 functions
KathManarang
 
Math functions, relations, domain & range
Math functions, relations, domain & rangeMath functions, relations, domain & range
Math functions, relations, domain & range
Renee Scott
 
Relations and Functions
Relations and FunctionsRelations and Functions
Relations and Functions
toni dimella
 
Module 1 Lesson 1 Remediation Notes
Module 1 Lesson 1 Remediation NotesModule 1 Lesson 1 Remediation Notes
Module 1 Lesson 1 Remediation Notes
toni dimella
 
Relations and functions
Relations and functionsRelations and functions
Relations and functions
Heather Scott
 
Relations and functions remediation notes
Relations and functions remediation notesRelations and functions remediation notes
Relations and functions remediation notes
carolinevest77
 
Storyboard math
Storyboard mathStoryboard math
Storyboard math
shandex
 
8.1 Relations And Functions
8.1 Relations And Functions8.1 Relations And Functions
8.1 Relations And Functions
Jessca Lundin
 
Relations and functions
Relations and functionsRelations and functions
Relations and functions
Rhea Rose Almoguez
 
Relations &amp; functions
Relations &amp; functionsRelations &amp; functions
Relations &amp; functions
chrystal_brinson
 
L1 functions, domain &amp; range
L1 functions, domain &amp; rangeL1 functions, domain &amp; range
L1 functions, domain &amp; range
James Tagara
 
7_Intro_to_Functions
7_Intro_to_Functions7_Intro_to_Functions
7_Intro_to_Functions
nechamkin
 
Relations & Functions
Relations & FunctionsRelations & Functions
Relations & Functions
J Edwards
 
Functions domainrange
Functions domainrangeFunctions domainrange
Functions domainrange
Dreams4school
 
Domain and range_ppt (1)
Domain and range_ppt (1)Domain and range_ppt (1)
Domain and range_ppt (1)
Bernard Mendoza
 
Functions domain-range
Functions domain-rangeFunctions domain-range
Functions domain-range
Olaug S
 
Relations and functions
Relations and functionsRelations and functions
Relations and functions
Thabani Masoka
 
Relations and Functions
Relations and FunctionsRelations and Functions
Relations and Functions
r3h1na
 
Functions
FunctionsFunctions
Functions
Ankit Bhandari
 
Ml lesson 4 8
Ml lesson 4 8Ml lesson 4 8
Ml lesson 4 8
marc_whitaker
 
Math functions, relations, domain & range
Math functions, relations, domain & rangeMath functions, relations, domain & range
Math functions, relations, domain & range
Renee Scott
 
Relations and Functions
Relations and FunctionsRelations and Functions
Relations and Functions
toni dimella
 
Module 1 Lesson 1 Remediation Notes
Module 1 Lesson 1 Remediation NotesModule 1 Lesson 1 Remediation Notes
Module 1 Lesson 1 Remediation Notes
toni dimella
 
Relations and functions
Relations and functionsRelations and functions
Relations and functions
Heather Scott
 
Relations and functions remediation notes
Relations and functions remediation notesRelations and functions remediation notes
Relations and functions remediation notes
carolinevest77
 
Storyboard math
Storyboard mathStoryboard math
Storyboard math
shandex
 
8.1 Relations And Functions
8.1 Relations And Functions8.1 Relations And Functions
8.1 Relations And Functions
Jessca Lundin
 
L1 functions, domain &amp; range
L1 functions, domain &amp; rangeL1 functions, domain &amp; range
L1 functions, domain &amp; range
James Tagara
 
7_Intro_to_Functions
7_Intro_to_Functions7_Intro_to_Functions
7_Intro_to_Functions
nechamkin
 
Relations & Functions
Relations & FunctionsRelations & Functions
Relations & Functions
J Edwards
 
Functions domainrange
Functions domainrangeFunctions domainrange
Functions domainrange
Dreams4school
 
Domain and range_ppt (1)
Domain and range_ppt (1)Domain and range_ppt (1)
Domain and range_ppt (1)
Bernard Mendoza
 
Functions domain-range
Functions domain-rangeFunctions domain-range
Functions domain-range
Olaug S
 
Relations and functions
Relations and functionsRelations and functions
Relations and functions
Thabani Masoka
 
Relations and Functions
Relations and FunctionsRelations and Functions
Relations and Functions
r3h1na
 

Similar to Linked List Leetcode - Easy Collections - Interview Questions Java (20)

LinkedList-VJ-V2.pptx Analysis of Algorithms and Data Structures
LinkedList-VJ-V2.pptx Analysis of Algorithms and Data StructuresLinkedList-VJ-V2.pptx Analysis of Algorithms and Data Structures
LinkedList-VJ-V2.pptx Analysis of Algorithms and Data Structures
ryadavrohit26
 
Linked list
Linked list Linked list
Linked list
Arbind Mandal
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
kasthurimukila
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
rozakashif85
 
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
AdrianEBJKingr
 
Linked list
Linked listLinked list
Linked list
maamir farooq
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
ssuserd2f031
 
Link list 2
Link list 2Link list 2
Link list 2
sana younas
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
footstatus
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
DaniyalAli81
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
Amar Jukuntla
 
package linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdfpackage linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdf
arcellzone
 
Linked lists
Linked listsLinked lists
Linked lists
Spencer Moran
 
LINKED LISTS
LINKED LISTSLINKED LISTS
LINKED LISTS
Dhrthi Nanda
 
Exception to indicate that Singly LinkedList is empty. .pdf
  Exception to indicate that Singly LinkedList is empty. .pdf  Exception to indicate that Singly LinkedList is empty. .pdf
Exception to indicate that Singly LinkedList is empty. .pdf
aravlitraders2012
 
13-Doubly Linked List data structure.pdf
13-Doubly Linked List data structure.pdf13-Doubly Linked List data structure.pdf
13-Doubly Linked List data structure.pdf
ssusere3b1a2
 
Linked Lists.pdf
Linked Lists.pdfLinked Lists.pdf
Linked Lists.pdf
Kaynattariq1
 
singly link list project in dsa.....by rohit malav
singly link list project in dsa.....by rohit malavsingly link list project in dsa.....by rohit malav
singly link list project in dsa.....by rohit malav
Rohit malav
 
Lecture ............ 3 - Linked Lists.pptx
Lecture ............ 3 - Linked Lists.pptxLecture ............ 3 - Linked Lists.pptx
Lecture ............ 3 - Linked Lists.pptx
SumeetRathi5
 
Datastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptxDatastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptx
kaleeswaric3
 
LinkedList-VJ-V2.pptx Analysis of Algorithms and Data Structures
LinkedList-VJ-V2.pptx Analysis of Algorithms and Data StructuresLinkedList-VJ-V2.pptx Analysis of Algorithms and Data Structures
LinkedList-VJ-V2.pptx Analysis of Algorithms and Data Structures
ryadavrohit26
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
kasthurimukila
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
rozakashif85
 
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
AdrianEBJKingr
 
1.3 Linked List.pptx
1.3 Linked List.pptx1.3 Linked List.pptx
1.3 Linked List.pptx
ssuserd2f031
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
footstatus
 
package linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdfpackage linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdf
arcellzone
 
Exception to indicate that Singly LinkedList is empty. .pdf
  Exception to indicate that Singly LinkedList is empty. .pdf  Exception to indicate that Singly LinkedList is empty. .pdf
Exception to indicate that Singly LinkedList is empty. .pdf
aravlitraders2012
 
13-Doubly Linked List data structure.pdf
13-Doubly Linked List data structure.pdf13-Doubly Linked List data structure.pdf
13-Doubly Linked List data structure.pdf
ssusere3b1a2
 
singly link list project in dsa.....by rohit malav
singly link list project in dsa.....by rohit malavsingly link list project in dsa.....by rohit malav
singly link list project in dsa.....by rohit malav
Rohit malav
 
Lecture ............ 3 - Linked Lists.pptx
Lecture ............ 3 - Linked Lists.pptxLecture ............ 3 - Linked Lists.pptx
Lecture ............ 3 - Linked Lists.pptx
SumeetRathi5
 
Datastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptxDatastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptx
kaleeswaric3
 
Ad

More from Sunil Yadav (7)

Tree Leetcode - Interview Questions - Easy Collections
Tree Leetcode - Interview Questions - Easy CollectionsTree Leetcode - Interview Questions - Easy Collections
Tree Leetcode - Interview Questions - Easy Collections
Sunil Yadav
 
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsLeet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Sunil Yadav
 
Bada Business, Dr. Vivek Bindra . Motivational Speaker (31 May)
Bada Business, Dr. Vivek Bindra . Motivational Speaker (31 May)Bada Business, Dr. Vivek Bindra . Motivational Speaker (31 May)
Bada Business, Dr. Vivek Bindra . Motivational Speaker (31 May)
Sunil Yadav
 
Golang, Future of Programming Language.
Golang, Future of Programming Language.Golang, Future of Programming Language.
Golang, Future of Programming Language.
Sunil Yadav
 
React Js Simplified
React Js SimplifiedReact Js Simplified
React Js Simplified
Sunil Yadav
 
LeetCode April Coding Challenge
LeetCode April Coding ChallengeLeetCode April Coding Challenge
LeetCode April Coding Challenge
Sunil Yadav
 
Docker with Micro Service and WebServices
Docker with Micro Service and WebServicesDocker with Micro Service and WebServices
Docker with Micro Service and WebServices
Sunil Yadav
 
Tree Leetcode - Interview Questions - Easy Collections
Tree Leetcode - Interview Questions - Easy CollectionsTree Leetcode - Interview Questions - Easy Collections
Tree Leetcode - Interview Questions - Easy Collections
Sunil Yadav
 
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsLeet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Sunil Yadav
 
Bada Business, Dr. Vivek Bindra . Motivational Speaker (31 May)
Bada Business, Dr. Vivek Bindra . Motivational Speaker (31 May)Bada Business, Dr. Vivek Bindra . Motivational Speaker (31 May)
Bada Business, Dr. Vivek Bindra . Motivational Speaker (31 May)
Sunil Yadav
 
Golang, Future of Programming Language.
Golang, Future of Programming Language.Golang, Future of Programming Language.
Golang, Future of Programming Language.
Sunil Yadav
 
React Js Simplified
React Js SimplifiedReact Js Simplified
React Js Simplified
Sunil Yadav
 
LeetCode April Coding Challenge
LeetCode April Coding ChallengeLeetCode April Coding Challenge
LeetCode April Coding Challenge
Sunil Yadav
 
Docker with Micro Service and WebServices
Docker with Micro Service and WebServicesDocker with Micro Service and WebServices
Docker with Micro Service and WebServices
Sunil Yadav
 
Ad

Recently uploaded (20)

Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Secure Access with Azure Active Directory
Secure Access with Azure Active DirectorySecure Access with Azure Active Directory
Secure Access with Azure Active Directory
VICTOR MAESTRE RAMIREZ
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Secure Access with Azure Active Directory
Secure Access with Azure Active DirectorySecure Access with Azure Active Directory
Secure Access with Azure Active Directory
VICTOR MAESTRE RAMIREZ
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 

Linked List Leetcode - Easy Collections - Interview Questions Java

  • 2. Linked List problems are relatively easy to master. Do not forget the Two-pointer technique, which not only applicable to Array problems but also Linked List problems as well. Another technique to greatly simplify coding in linked list problems is the dummy node trick. We recommend: Reverse Linked List, Merge Two Sorted Lists and Linked List Cycle. For additional challenge, solve these problems recursively: Reverse Linked List, Palindrome Linked List and Merge Two Sorted Lists. IntroductION
  • 3. Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Given linked list -- head = [4,5,1,9], which looks like following: Example 1: Input: head = [4,5,1,9], node = 5 Output: [4,1,9] Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function. Example 2: Input: head = [4,5,1,9], node = 1 Output: [4,5,9] Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function. Note: • The linked list will have at least two elements. • All of the nodes' values will be unique. • The given node will not be the tail and it will always be a valid node of the linked list. • Do not return anything from your function. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public void deleteNode(ListNode node) { ListNode Prev = null; while(node.next!=null){ node.val = node.next.val; Prev = node; node = node.next; } if(Prev!=null) Prev.next = null; } }
  • 4. Remove Nth Node From End of List Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. Follow up: Could you do this in one pass? • /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode Prev = null; ListNode fN = head; ListNode slow = head; int counter = 1; while(head!=null){ if(counter>n){ Prev = slow; slow = slow.next; } counter++; head = head.next; } if(Prev!=null){ if(slow!=null) Prev.next = slow.next; else Prev.next = null; } if(Prev == null){ return fN.next; } return fN; } }
  • 5.  Reverse Linked List Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? • /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode reverseList(ListNode head) { ListNode root = head; ListNode Prev = null; while(head!=null){ ListNode nextNode = head.next; head.next = Prev; Prev = head; head = nextNode; } return Prev; } }
  • 6. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode x = l1; ListNode y = l2; ListNode result = new ListNode(-1); ListNode fR = result; while(x!=null && y!=null){ if(x.val <= y.val){ result.next = new ListNode(x.val); x = x.next; }else{ result.next = new ListNode(y.val); y = y.next; } result = result.next; } if(x!=null){ result.next = x; result = result.next; } if(y!=null){ result.next = y; result = result.next; } return fR.next; }
  • 7. Palindrome Linked List Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false Example 2: Input: 1->2->2->1 Output: true Follow up:
 Could you do it in O(n) time and O(1) space? /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public boolean isPalindrome(ListNode head) { ListNode slow = head; ListNode fast = head; while(fast!=null && fast.next!=null){ slow = slow.next; fast = fast.next.next; } Stack<ListNode> stack = new Stack<>(); // Slow is at Middle Position while(slow!=null){ stack.push(slow); slow = slow.next; } while(!stack.isEmpty()){ if(head.val!=stack.pop().val){ return false; } head = head.next; } return true; } }
  • 8. Linked List Cycle Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list. Example 1: Input: head = [3,2,0,-4], pos = 1 Output: true Explanation: There is a cycle in the linked list, where tail connects to the second node. /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { if(head == null){ return false; } ListNode slow = head; ListNode fast = head.next; while(true){ if(slow==null || fast==null){ return false; } if(slow==fast){ return true; } slow = slow.next; if(fast.next==null){ return false; } fast = fast.next.next; } } }