Data Structures and Algorithms Using Java
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
1. Floyd’s Cycle-141
Given head, the head of a linked list, determine if the linked list has a
cycle in it.
while(rabit!=null&&rabit.next!=null){
turtle=turtle.next;
rabit=rabit.next.next;
if(turtle==rabit){
return true;
}
return false;
}
}
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;
}
}
class Solution {
public ListNode reverseBetween(ListNode head, int m, int
n) {
if(m==n) return head;
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;
return head;
}
}
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;
}
}
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;
}
}
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;
}
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;
}
}
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;
}
}