0% found this document useful (0 votes)
18 views

Data Structures and Algorithms Using Java

The document discusses various algorithms and data structures related to linked lists. It covers topics like detecting cycles in linked lists, reversing linked lists, removing elements from linked lists, checking if a linked list is a palindrome, finding the intersection of two linked lists, and adding two numbers represented as linked lists.

Uploaded by

kanishkap.20eie
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Data Structures and Algorithms Using Java

The document discusses various algorithms and data structures related to linked lists. It covers topics like detecting cycles in linked lists, reversing linked lists, removing elements from linked lists, checking if a linked list is a palindrome, finding the intersection of two linked lists, and adding two numbers represented as linked lists.

Uploaded by

kanishkap.20eie
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Structures and Algorithms using Java

LINKED LISTS

1. Floyd’s Cycle-141
2. Return the Node where the Cycle Begins in Linked Lists-142
3. Reverse a Linked Lists-206
4. Reversing a Linked Lists from certain position(m) to certain
position(n)- 92
5. Remove an element from the List-203
6. Odd-even Linked Lists-328
7. Remove Nodes from Linked Lists(Remove a node which has a Node
with a strictly greater value anywhere to the right side of it)-2487
8. Check whether the given Linked List is Palindrome or not-234
9. Intersection of Linked List (return the intersection point of two
Linked Lists)
10. Adding two Numbers from Linked Lists

BASIC ALGORITHMS FOR LINKED LISTS

1.Traverse+ Optional Collections is applicable where the problem


needs you to traverse the given Linked Lists from start to end ,while
validating or finding something ,for eg:Adding two numbers represented
by Linked Lists.
2.Two Pointers Approach is applicable when the problem needs you to
Perform an operation,given Nth Node from the end of the lists
Lists is cycle
Given are two lists that are merged
3.Link Manipulation is applicable when the problem needs you to
Modify the given Linked Lists in place,for-eg : reverse a Linked
Lists
Perform an Operation on two Linked Lists,for eg: merge two sorted
Linked Lists to give a single sorted List.
4.Recursion is applicable when the problem needs you to perform the
same operation till the base condition is reached ,for eg: reverse a linked
Lists in groups of given size
5.k-way merge is applicable when the problems needs you to given
multiple sorted Linked Lists,merge them into one sorted lists
Solutions

1. Floyd’s Cycle-141

Given head, the head of a linked list, determine if the linked list has a
cycle in it.

public class Solution {


public boolean hasCycle(ListNode head) {
if(head==null ||head.next==null){
return false;
}
ListNode rabit=head;
ListNode turtle=head;

while(rabit!=null&&rabit.next!=null){
turtle=turtle.next;
rabit=rabit.next.next;

if(turtle==rabit){
return true;
}
return false;
}
}

2.Return the Node where the Cycle Begins in Linked Lists-142


public class Solution {
public ListNode detectCycle(ListNode head) {
if(head==null||head.next==null){
return null;
}
ListNode turtle=head;
ListNode rabit=head;
ListNode initial=head;
while(rabit.next!=null&&rabit.next.next!=null){
turtle=turtle.next;
rabit=rabit.next.next;
if(turtle==rabit){
while(turtle!=initial){
turtle=turtle.next;
initial=initial.next;
}
return initial;
}
}
return null;

3.Reverse a Linked Lists-206

class Solution {
public ListNode reverseList(ListNode head) {
if(head==null){
return null;
}
if(head.next==head){
return head;
}
ListNode prev=null;
ListNode current=head;
ListNode next=head.next;
ListNode temp=head;

while(current!=null){
next=current.next;
current.next=prev;
prev=current;
current=next;
}
head=prev;
return prev;
}
}

4.Reversing a Linked Lists from certain position(m) to certain


position(n)- 92

class Solution {
public ListNode reverseBetween(ListNode head, int m, int
n) {
if(m==n) return head;

ListNode prev = null;//track (m-1)th node


ListNode first = new ListNode(0);//first's next points to
mth
ListNode second = new ListNode(0);//second's next points
to (n+1)th

int i=0;
ListNode p = head;
while(p!=null){
i++;
if(i==m-1){
prev = p;
}

if(i==m){
first.next = p;
}

if(i==n){
second.next = p.next;
p.next = null;
}

p= p.next;
}
if(first.next == null)
return head;
ListNode p1 = first.next;
ListNode p2 = p1.next;
p1.next = second.next;

while(p1!=null && p2!=null){


ListNode t = p2.next;
p2.next = p1;
p1 = p2;
p2 = t;
}
if(prev!=null)
prev.next = p1;
else
return p1;

return head;
}
}

5.Delete an element from the List-203

class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head==null){
return head;
}
while(head != null && head.val == val){
head = head.next;
}
ListNode temp=head;
while(temp!=null&&temp.next!=null)
if(temp.next.val==val){
temp.next=temp.next.next;
}
else {
temp=temp.next;
}
return head;
}
}

6.Odd-even Linked Lists-328


class Solution {
public ListNode oddEvenList(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode odd=head;
ListNode even=head.next;

ListNode oddcurrent=odd;
ListNode evencurrent=even;
while(evencurrent!=null&&evencurrent.next!=null){
oddcurrent.next=oddcurrent.next.next;
evencurrent.next=evencurrent.next.next;

oddcurrent=oddcurrent.next;
evencurrent=evencurrent.next;
}
oddcurrent.next=even;
return odd;

}
}

7.Remove Nodes from Linked Lists(Remove a node which has a Node


with a strictly greater value anywhere to the right side of it)-2487
class Solution {
public ListNode removeNodes(ListNode head) {
if(head==null){
return head;
}
ListNode temp=head;
ListNode prev=null;
head.next = removeNodes(head.next);
return head.next != null && head.val < head.next.val ?
head.next : head;
}
}

8. Check whether the given Linked List is Palindrome or not-234

class Solution {
public boolean isPalindrome(ListNode head) {
if(head==null){
return true;
}
if(head.next==head){
return true;
}
ListNode turtle=head;
ListNode rabit=head;
while(rabit!=null&&rabit.next!=null){
turtle=turtle.next;
rabit=rabit.next.next;
}
ListNode l=head;
ListNode r=reverse(turtle);

while(r!=null){
if(l.val==r.val){
l=l.next;
r=r.next;
}
else
return false;
}
return true;
}
private ListNode reverse(ListNode head){
if(head==null||head.next==null){
return head;
}
ListNode prev=null;
ListNode current=head;
ListNode next=head.next;
while(current!=null){
next=current.next;
current.next=prev;
prev=current;
current=next;
}
head=prev;
return prev;
}

9. Intersection of Linked List (return the intersection point of two


Linked Lists)-160
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)){
if(temp1==null){
temp1=headB;
}else{
temp1=temp1.next;
}
if(temp2==null){
temp2=headA;
}
else{
temp2=temp2.next;
}
}
return temp1;
}
}

10. Adding two Numbers from Linked Lists-2

class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int v1=0,v2=0,sum=0,carry=0;
ListNode first=new ListNode(-1);
ListNode ptr =first;
while(l1!=null|| l2!=null){
if(l1!=null){
v1=l1.val;
l1=l1.nexlt;
}
else{
v1=0;}
if(l2!=null){
v2=l2.val;
l2=l2.next;
}
else{
v2=0;}
sum=v1+v2+carry;
carry=sum/10;
sum=sum%10;
ListNode temp=new ListNode(sum);
ptr.next=temp;
ptr=ptr.next;
}
if(carry!=0){
ListNode temp=new ListNode(carry);
ptr.next=temp;
}
return first.next;
}
}

You might also like