Question 1. Sum elements from two Lists
*Input:(2 -> 4 -> 3) + (5 -> 6 -> 4)
Output:7 -> 0 -> 8
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode p1 = l1;
ListNode p2 = l2;
int carry = 0;
ListNode curr = head;
while(p1 != null || p2 != null){
int sum = (p1 == null ? 0 : p1.val) + (p2 == null ? 0 : p2.val) + carry;
if(sum>=10){
carry =1;
}else{
carry = 0;
}
curr.next = new ListNode(sum%10);
p1 = p1!=null ? p1.next : null;
p2 = p2!=null ? p2.next : null;
curr = curr.next;
}
if(carry >0){
curr.next= new ListNode(carry);
}
return head.next;
}
}
Question 2. Input elements at Kth location on a single liked array
/*
Insert Node at a given position in a linked list
head can be NULL
First element in the linked list is at position 0
Node is defined as
class Node {
int data;
Node next;
}
*/
Node InsertNth(Node head, int data, int position) {
Node n = new Node();
n.data =data;
if(position == 0){
n.next = head;
head = n;
}else{
Node pos = head;
while(position >1){
pos = pos.next;
position--;
}
n.next = pos.next;
pos.next = n;
}
return head;
}
Question 3. Delete elements at Kth location on a single liked array
/*
Delete Node at a given position in a linked list
head pointer input could be NULL as well for empty list
Node is defined as
class Node {
int data;
Node next;
}
*/
Node Delete(Node head, int position) {
if(position == 0){
head = head.next;
}else{
Node prev = head;
while(position > 1){
prev = prev.next;
position--;
}
Node target= prev.next;
prev.next = target.next;
target.next =null;
}
return head;
}
Question 4. Reverse linked list
/*
Print elements of a linked list in reverse order
head pointer input could be NULL as well for empty list
Node is defined as
class Node {
int data;
Node next;
}
*/
void ReversePrint(Node head) {
Node cur = head;
Node prev= null;
while(cur != null){
Node temp = cur.next;
cur.next = prev;
prev = cur;
cur = temp;
}
while(prev != null){
System.out.println(prev.data);
prev = prev.next;
}
}
Question 5. Delete dup node
/*
Node is defined as
class Node {
int data;
Node next;
}
*/
Node RemoveDuplicates(Node head) {
Node cur = head;
while(cur.next != null){
Node temp =cur.next;
if(cur.data == temp.data){
cur.next = temp.next;
continue;
}
cur = temp;
}
return head;
}
Question 6. Detect a list is circular or not
- use Floyd circule finding algorithm.
/*
Detect a cycle in a linked list. Note that the head pointer may be 'null' if the list is empty.
A Node is defined as:
class Node {
int data;
Node next;
}
*/
boolean hasCycle(Node head) {
if(head == null){
return false;
}
Node p1 = head.next;
Node p2 = head.next.next;
while(p1 != null && p2 != null){
if(p1 == p2){
return true;
}
p1 = p1.next;
p2 = p2.next.next;
}
return false;
}
Question 7. Find a merge point between two lists.
int FindMergeNode(Node headA, Node headB) {
Node curA = headA;
Node curB = headB;
int cntA = 0;
int cntB = 0;
while(curA != null || curB != null){
if(curA != null){
cntA++;
curA = curA.next;
}
if(curB != null){
cntB++;
curB = curB.next;
}
}
int diff = Math.abs(cntA - cntB);
curA = headA;
curB = headB;
if(cntA > cntB){
while(diff >0){
curA = curA.next;
diff--;
}
}else if(cntA < cntB){
while(diff >0){
curB = curB.next;
diff--;
}
}
while(curA != curB){
curA = curA.next;
curB = curB.next;
}
return curA.data;
}
Question 8. Insert element in double linked list in ascending order
Node SortedInsert(Node head,int data) {
if(head == null){
head.data = data;
return head;
}
Node cur = head;
Node n = new Node();
n.data = data;
while(cur !=null){
int prev = cur.prev == null ? Integer.MIN_VALUE : cur.prev.data;
int next = cur.next == null ? Integer.MAX_VALUE : cur.next.data;
if(prev < data && data < cur.data){
n.next = cur;
n.prev = cur.prev;
cur.prev = n;
if(cur.prev != null){
cur.prev.next= n;
}
break;
}
if(cur.data <= data && data < next){
n.next = cur.next;
n.prev = cur;
cur.next = n;
if(cur.next.prev != null){
cur.next.prev= n;
}
break;
}
cur = cur.next;
}
return head;
}