Showing posts with label LinkedList. Show all posts
Showing posts with label LinkedList. Show all posts

Wednesday, April 3, 2019

How do you add two numbers represented by linked lists ??

Problem statement:
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.


Example:


Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

class Node {
    int data;
    Node next;

    Node(int data) {

        this.data = data;
    }

    Node() {

    }

    @Override

    public String toString() {
        return "Node{" +
                "data=" + data +
                ", next=" + next +
                '}';
    }
}

public class AddTwoNumbers {

    static Node head = null;

    public static void main(String[] args) {

        Node n1 = new Node(1);
        Node n2 = new Node(2);
        Node n3 = new Node(3);

        n1.next = n2;

        n2.next = n3;

        Node n4 = new Node(1);

        Node n5 = new Node(2);
        Node n6 = new Node(3);

        n4.next = n5;

        n5.next = n6;

        Node result = calculate(n1, n4);

        System.out.println(result);
    }

    static Node calculate(Node a, Node b) {

        //        //FIXME: Write your logic
        //  1, 2, 3
        //  1, 2, 3
        int carry = 0;

        while (a != null && b != null) {

            addLast(a.data + b.data);

            a = a.next;

            b = b.next;
        }
        return head;
    }

    public static void addLast(int data) {


        if (head == null) {

            head = new Node(data);
            return;
        }
        Node last = head;
        while (last.next != null) {
            last = last.next;
        }
        last.next = new Node(data);
    }
}

Output:
Node{data=2, next=Node{data=4, next=Node{data=6, next=null}}}

Tuesday, May 1, 2018

Reverse a LinkedList

Problem statement: Reverse a LinkedList in java !
  1. class Node5 {
  2. Node5 next;
  3. int data;

  4. Node5(int data) {
  5. this.data = data;
  6. }
  7. } // end of Node5

  8. public class ReverseLinkedList {

  9. Node5 head;

  10. public static void main(String[] args) {
  11. ReverseLinkedList l = new ReverseLinkedList();
  12. l.add(3);
  13. l.add(5);
  14. l.add(8);
  15. l.add(9);
  16. l.print(l.head);
  17. l.reverse();
  18. System.out.println("\nafter reverse linked list");
  19. l.print(l.head);
  20. } // end of main()

  21. // add the element in LinkedList
  22. public void add(int data) {
  23. Node5 next = new Node5(data);
  24. if (head == null) {
  25. head = next;
  26. return;
  27. }
  28. Node5 last = head;
  29. while (last.next != null) {
  30. last = last.next;
  31. }
  32. last.next = next;
  33. } // end of add()

  34. // print the LinkedList
  35. void print(Node5 next) {
  36. Node5 temp = next;
  37. while (temp != null) {
  38. System.out.print(temp.data + "-->");
  39. temp = temp.next;
  40. }
  41. } // end of print()

  42. // reverse the LinkedList
  43. Node5 reverse() {
  44. Node5 prev = null;
  45. Node5 current = head;
  46. Node5 next = null;
  47. while (current != null) {
  48. next = current.next;
  49. current.next = prev;
  50. prev = current;
  51. current = next;
  52. }
  53. head = prev;
  54. return head;
  55. } // end of reverse()
  56. } //end of ReverseLinkedList 
Output: 
3-->5-->8-->9-->
after reverse linked list
9-->8-->5-->3-->

LinkedList implementation

Problem statement: LinkedList implementation in java !!
  1. class A {
  2. A n;
  3. int data;
  4. A(int data) {
  5. this.data = data;
  6. }
  7. }
  8. public class LinkedList {
  9. A head;

  10. /*public static void main(String[] args) {

  11. add(1);
  12. add(2);
  13. print(head);
  14. */

  15. /*
  16. A head = new A(1);
  17. A h1 = new A(2);
  18. A h2 = new A(3);
  19. A h3 = new A(4);
  20. A h4 = new A(5);
  21. A h5 = new A(6);

  22. head.n=h1; h1.n=h2; h2.n=h3;h3.n=h4;h4.n=h5;//h5.n=null;

  23. print(head);

  24. /*A head = new A();
  25. head.data=1;
  26. head.n=null;

  27. A h1 = new A();
  28. h1.data=2;
  29. h1.n=null;

  30. head.n=h1;

  31. A h2 = new A();
  32. h2.data=3;
  33. h2.n=null;

  34. h1.n=h2;

  35. A h3 = new A();
  36. h3.data=4;
  37. h3.n=null;

  38. h2.n=h3;

  39. A h4 = new A();
  40. h4.data=5;
  41. h4.n=null;

  42. h3.n=h4;
  43. */
  44. /* System.out.println(head.data); h.data;
  45. System.out.println(head.n.data);h=h.n; h.data;
  46. System.out.println(head.n.n.data); h=h.n.n;h.data;
  47. System.out.println(head.n.n.n.data);h=h.n.n.n;h.data;
  48. System.out.println(head.n.n.n.n.data);h=h.n.n.n.n;h.data;

  49. print(head);
  50. */
  51. //}

  52. // print the LinkedList
  53. public  void print(A head){
  54. A temp=head;
  55. while(temp!=null){
  56. System.out.println(temp.data);
  57. temp=temp.n;
  58. }
  59. }

  60. // add the element in LinkedList
  61. public void add(int data) {
  62. A new_node = new A(data);

  63. if (head == null) {
  64. head = new_node;
  65. return;
  66. }
  67. A last = head;
  68. while (last.n != null) {
  69. last = last.n;
  70. }
  71. last.n = new_node;
  72. }

  73. // reverse the LinkedList
  74. A reverse() {
  75. A prev = null;
  76. A current = head;
  77. A next = null;
  78. while (current != null) {
  79. next = current.n;
  80. current.n = prev;
  81. prev = current;
  82. current = next;
  83. }
  84. head = prev;
  85. return head;
  86. }
  87. }
MainUser class:

  1. public class MyLinkedList {

  2. public static void main(String[] args) {
  3. LinkedList l = new LinkedList();
  4. l.add(1);
  5. l.add(2);

  6. l.print(l.head);

  7. l.reverse();
  8. System.out.println("-----------");
  9. l.print(l.head);
  10. }
  11. }
Output:
1
2
-----------
2
1

LinkedList high level implementation [i]

Problem statement: LinkedList high level implementation in java !
  1. class Node4 {
  2. Node4 next;
  3. int data;

  4. Node4(int data) {
  5. this.data = data;
  6. }
  7. }
  8. public class LinkedListHighLevelExe1 {
  9. Node4 head;

  10. public static void main(String[] args) {
  11. LinkedListHighLevelExe1 l = new LinkedListHighLevelExe1();
  12. l.add(3);
  13. l.add(5);
  14. l.add(8);
  15. l.add(9);
  16. l.print(l.head);
  17. }

  18. // add the element in LinkedList
  19. public void add(int data) {
  20. Node4 next = new Node4(data);
  21. if (head == null) {
  22. head = next;
  23. return;
  24. }
  25. Node4 last = head;
  26. while (last.next != null) {
  27. last = last.next;
  28. }
  29. last.next = next;
  30. }
  31. // print the LinkedList
  32. void print(Node4 next) {
  33. Node4 temp = next;
  34. while (temp != null) {
  35. System.out.print(temp.data + "-->");
  36. temp = temp.next;
  37. }
  38. }
  39. }
Output:
3-->5-->8-->9-->

LinkedList low level implementation [v]

Problem statement: LinkedList low level implementation in java !
  1. class Node3 {
  2. Node3 next;
  3. int data;

  4. Node3(int data) {
  5. this.data = data;
  6. }
  7. }

  8. public class LinkedListLowLevelExe5 {
  9. public static void main(String[] args) {

  10. Node3 head = new Node3(9);
  11. Node3 h2 = new Node3(2);
  12. Node3 h3 = new Node3(4);
  13. Node3 h4 = new Node3(5);
  14. Node3 h5 = new Node3(8);
  15. Node3 h6 = new Node3(9);

  16. // linking the obj reference with the next
  17. head.next = h2;
  18. h2.next = h3;
  19. h3.next = h4;
  20. h4.next = h5;
  21. h5.next = h6;
  22. // h6.next = null;

  23. print(head);

  24. }

  25. static void print(Node3 next) {
  26. Node3 temp = next;
  27. while (temp != null) {
  28. System.out.print(temp.data + "-->");
  29. temp = temp.next;
  30. }
  31. }
  32. }
Output:
9-->2-->4-->5-->8-->9-->

LinkedList low level implementation [iv]

Problem statement: LinkedList low level implementation in java !

  1. class Node2 {
  2. Node2 next;
  3. int data;

  4. Node2(int data) {
  5. this.data = data;
  6. }
  7. }

  8. public class LinkedListLowLevelExe4 {
  9. public static void main(String[] args) {
  10. Node2 head = new Node2(4);
  11. Node2 h2 = new Node2(5);
  12. Node2 h3 = new Node2(6);
  13. Node2 h4 = new Node2(8);

  14. head.next = h2;
  15. h2.next = h3;
  16. h3.next = h4;
  17. h4.next = null;
  18. print(head);
  19. }

  20. public static void print(Node2 next) {
  21. Node2 temp = next;
  22. while (temp != null) {
  23. System.out.print(temp.data + "-->");
  24. temp = temp.next;
  25. }
  26. }
  27. }

Output:
4-->5-->6-->8-->

LinkedList low level implementation [iii]

Problem statement: LinkedList low level implementation in java !
  1. class Node1 {
  2. Node1 next;
  3. int data;

  4. Node1(int data) {
  5. this.data = data;
  6. }
  7. }

  8. public class LinkedListLowLevelExe3 {
  9. public static void main(String[] args) {
  10. Node1 head = new Node1(2);
  11. Node1 h2 = new Node1(5);
  12. Node1 h3 = new Node1(6);
  13. Node1 h4 = new Node1(3);
  14. Node1 h5 = new Node1(9);

  15. head.next = h2;
  16. h2.next = h3;
  17. h3.next = h4;
  18. h4.next = h5;
  19. h5.next = null;

  20. System.out.print(head.data + "-->");
  21. System.out.print(head.next.data + "-->");
  22. System.out.print(head.next.next.data + "-->");
  23. System.out.print(head.next.next.next.data + "-->");
  24. System.out.print(head.next.next.next.next.data+"-->NULL");
  25. }
  26. }

Output:
2-->5-->6-->3-->9-->NULL

LinkedList low level implementaion [ii]

Problem statement: LinkedList low level implementation in java !
  1. class Node {
  2. Node next;
  3. int data;
  4. }

  5. public class LinkedListLowLevelExe2 {
  6. public static void main(String[] args) {

  7. Node head = new Node();
  8. head.next = null;
  9. head.data = 1;

  10. Node h2 = new Node();
  11. h2.next = null;
  12. h2.data = 2;

  13. Node h3 = new Node();
  14. h3.next = null;
  15. h3.data = 5;

  16. Node h4 = new Node();
  17. h4.next = null;
  18. h4.data = 8;

  19. Node h5 = new Node();
  20. h5.next = null;
  21. h5.data = 9;

  22. head.next = h2;
  23. h2.next = h3;
  24. h3.next = h4;
  25. h4.next = h5;
  26. h5.next = null;

  27. System.out.print(head.data + "-->"); // head.data
  28. System.out.print(head.next.data + "-->"); // head = head.next; head.data
  29. System.out.print(head.next.next.data + "-->"); // head = head.next.next; head.data
  30. System.out.print(head.next.next.next.data+"-->"); // head = head.next.next.next; head.data
  31. System.out.print(head.next.next.next.next.data); // head = head.next.next..next.next; head.data
  32. }
  33. }
Output:
1-->2-->5-->8-->9

LinkedList low level implementation [i]

Problem statement: LinkedList low level implementation in java !
  1. class A1 {
  2. A1 n;
  3. int data;
  4. }

  5. public class LinkedListLowLevelExe1 {
  6. A head;

  7. public static void main(String[] args) {

  8. A1 head = new A1();
  9. head.data = 1;
  10. head.n = null;

  11. A1 h1 = new A1();
  12. h1.data = 2;
  13. h1.n = null;

  14. head.n = h1;

  15. A1 h2 = new A1();
  16. h2.data = 3;
  17. h2.n = null;

  18. h1.n = h2;

  19. A1 h3 = new A1();
  20. h3.data = 4;
  21. h3.n = null;

  22. h2.n = h3;

  23. A1 h4 = new A1();
  24. h4.data = 5;
  25. h4.n = null;

  26. h3.n = h4;
  27. System.out.println(head.data); // h.data;
  28. System.out.println(head.n.data); // h=h.n; h.data;
  29. System.out.println(head.n.n.data); // h=h.n.n;h.data;
  30. System.out.println(head.n.n.n.data); // h=h.n.n.n;h.data;
  31. System.out.println(head.n.n.n.n.data); // h=h.n.n.n.n;h.data;
  32. }
  33. }
Output:
1
2
3
4
5

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...