Exemplo n.º 1
0
 public void setPrevious(LinkListNode p)
 {
     prev = p;
     if (p != null && p.next != this)
     {
         p.setNext(this);
     }
 }
Exemplo n.º 2
0
 public LinkListNode clone()
 {
     LinkListNode next2 = null;
     if (next != null)
     {
         next2 = next.clone();
     }
     LinkListNode head2 = new LinkListNode(data, next2, null);
     return head2;
 }
Exemplo n.º 3
0
        public LinkListNode clone()
        {
            LinkListNode next2 = null;

            if (next != null)
            {
                next2 = next.clone();
            }
            LinkListNode head2 = new LinkListNode(data, next2, null);

            return(head2);
        }
Exemplo n.º 4
0
 public void setNext(LinkListNode n)
 {
     next = n;
     if (this == last)
     {
         last = n;
     }
     if (n != null && n.prev != this)
     {
         n.setPrevious(this);
     }
 }
Exemplo n.º 5
0
 public void setNext(LinkListNode n)
 {
     next = n;
     if (this == last)
     {
         last = n;
     }
     if (n != null && n.prev != this)
     {
         n.setPrevious(this);
     }
 }
Exemplo n.º 6
0
        //method 2, recursion. O(n) time, O(n) space, start with counter = 0
        static LinkListNode kthToLastEleRecursive(LinkListNode head, int k, Index idx) {
            if (head == null) {
                return null;
            }
            LinkListNode node = kthToLastEleRecursive(head.next, k, idx);

            idx.value += 1;
            if (idx.value == k)
            {
                return head;
            }

            return node;
        }
Exemplo n.º 7
0
        //method 2, recursion. O(n) time, O(n) space, start with counter = 0
        static LinkListNode kthToLastEleRecursive(LinkListNode head, int k, Index idx)
        {
            if (head == null) {
                return null;
            }
            LinkListNode node = kthToLastEleRecursive(head.next, k, idx);

            idx.value += 1;
            if (idx.value == k)
            {
                return head;
            }

            return node;
        }
Exemplo n.º 8
0
        //mehtod 3, 2 pointers, iterative, O(n) time
        static LinkListNode kthToLastEleTwoPointer(LinkListNode head, int k) {
            LinkListNode p1 = head;
            LinkListNode p2 = head;

            for (int i = 0; i < k; i++) {
                if (p2 == null)
                    return null;
                p2 = p2.next;
            }

            while (p2 != null) {
                p1 = p1.next;
                p2 = p2.next;
            }

            return p1;         
        }
Exemplo n.º 9
0
        //mehtod 3, 2 pointers, iterative, O(n) time
        static LinkListNode kthToLastEleTwoPointer(LinkListNode head, int k)
        {
            LinkListNode p1 = head;
            LinkListNode p2 = head;

            for (int i = 0; i < k; i++) {
                if (p2 == null)
                    return null;
                p2 = p2.next;
            }

            while (p2 != null) {
                p1 = p1.next;
                p2 = p2.next;
            }

            return p1;
        }
Exemplo n.º 10
0
    {//return kth to last: implement an algorithm to find the kth to last elment of a single linked list. 

        static void Main(string[] args)
        {
            //need test later
            
            LinkListNode first = new LinkListNode(0, null, null); 
            LinkListNode head = first;
            LinkListNode second = first;
            for (int i = 1; i < 7; i++)
            {
                second = new LinkListNode(i, null, null);
                first.setNext(second);
                second.setPrevious(first);
                first = second;
            }
      
            for (int i = 0; i <= 7 + 1; i++)
            {
                //Console.WriteLine(kthToLastEleTwoPointer(head, i).data);
                LinkListNode node = kthToLastEle(head, i);
                String nodeValue = node == null ? "null" : "" + node.data;
                Console.WriteLine(nodeValue);
            }
        }
Exemplo n.º 11
0
        //method 1, straghit forward, get length, index = length - k + 1, return element at index. O(N) time and O(1) space
        static LinkListNode kthToLastEle(LinkListNode head, int k) {
            LinkListNode n = head;
            int length = 1;

            while (n.next != null) {
                n = n.next;
                length++;
            }

            int index = length - k - 1;

            length = 0 ;
            n = head;
            while (n != null) {
                if (length == index)
                    return n;

                n = n.next;
                length++;
            }

            return null;
        }
Exemplo n.º 12
0
        //method 1, straghit forward, get length, index = length - k + 1, return element at index. O(N) time and O(1) space
        static LinkListNode kthToLastEle(LinkListNode head, int k)
        {
            LinkListNode n = head;
            int length = 1;

            while (n.next != null) {
                n = n.next;
                length++;
            }

            int index = length - k - 1;

            length = 0 ;
            n = head;
            while (n != null) {
                if (length == index)
                    return n;

                n = n.next;
                length++;
            }

            return null;
        }
Exemplo n.º 13
0
 public LinkListNode(int d, LinkListNode n, LinkListNode p)
 {
     data = d;
     setNext(n);
     setPrevious(p);
 }
Exemplo n.º 14
0
        //return kth to last: implement an algorithm to find the kth to last elment of a single linked list.
        static void Main(string[] args)
        {
            //need test later

            LinkListNode first = new LinkListNode(0, null, null);
            LinkListNode head = first;
            LinkListNode second = first;
            for (int i = 1; i < 7; i++)
            {
                second = new LinkListNode(i, null, null);
                first.setNext(second);
                second.setPrevious(first);
                first = second;
            }

            for (int i = 0; i <= 7 + 1; i++)
            {
                //Console.WriteLine(kthToLastEleTwoPointer(head, i).data);
                LinkListNode node = kthToLastEle(head, i);
                String nodeValue = node == null ? "null" : "" + node.data;
                Console.WriteLine(nodeValue);
            }
        }
Exemplo n.º 15
0
 static LinkListNode kthToLastEleRecursive(LinkListNode head, int k)
 {
     Index idx = new Index();
     return kthToLastEleRecursive(head, k, idx);
 }
Exemplo n.º 16
0
 public LinkListNode(int d, LinkListNode n, LinkListNode p)
 {
     data = d;
     setNext(n);
     setPrevious(p);
 }
Exemplo n.º 17
0
 static LinkListNode kthToLastEleRecursive(LinkListNode head, int k) {
     Index idx = new Index();
     return kthToLastEleRecursive(head, k, idx);
 }