Sunday, September 8, 2013

Remove K nodes from back and add in front in Link List.

WAP such that remove K elements from back and add in front.
Example:
Sample Input : 
Link list :10->20->100->30->50->5->70->234
Given K : 3
Sample Output:
5->70->234->10->20->100->30->50
Algo:
  1. Take two pointer both pointing head at beginning.
  2. Move firstPointer to K node.
  3. Now move both first and second pointer while firstPointer != null.
  4. Here secondPointer will point to Kth Node from Last.
  5. Make node of secondPointer as head of Link list.

Source Code:

Addition of integers given in two linklist.

Problem: You have two numbers represented by a linked list, where each node contains a sin-gle digit The digits are stored in reverse order, such that the 1s digit is at the head of the list Write a function that adds the two numbers and returns the sum as a linked list
EXAMPLE 
Sample Input: (3 -> 1 -> 5) + (5 -> 9 -> 2)
Sample Output: 8 -> 0 -> 8

Sample Input:
Integer 1: 1 1 5 
Integer 2: 1 9 3 
Sample Output:
Result: 2 0 9

Algorithm:

Source Code: