Exemplo n.º 1
0
        public NodeLL SwapPairs(NodeLL head)
        {
            if (head == null || head.next == null)
            {
                return(head);
            }
            NodeLL node = head;

            head = node.next; // remember head node;
            NodeLL prevNode = null;

            while (node != null)
            {
                NodeLL nextPair = null;
                if (node.next != null)
                {
                    nextPair = node.next.next; // remember next pair start
                    node     = SwapPair(node); // swap current pair
                }
                if (prevNode != null)
                {
                    prevNode.next = node; // link prev pair to current
                }
                prevNode = node.next;     // remember current pair end
                node     = nextPair;
            }
            return(head);
        }
Exemplo n.º 2
0
        NodeLL SwapPair(NodeLL node)
        {
            // assume node!=null and node.next!=null
            NodeLL nextNode     = node.next;     // node 2
            NodeLL nextNextNode = nextNode.next; // node 3

            node.next     = nextNextNode;        //link node 1 to 3
            nextNode.next = node;                // link node 2 to 1
            return(nextNode);                    // return node 2
        }
Exemplo n.º 3
0
        public NodeLL Swap(NodeLL head)
        {
            NodeLL node         = head;
            NodeLL nextNode     = node.next;
            NodeLL nextnextNode = nextNode.next;

            node.next     = nextnextNode;
            nextNode.next = node;
            return(nextNode);
        }
Exemplo n.º 4
0
        public NodeLL MergeK(NodeLL [] list)
        {
            NodeLL ptr = new NodeLL(0);
            NodeLL res = ptr;

            for (int i = 0; i < list.Length; i++)
            {
                ptr = Merge(ptr, list[i]);
            }
            return(ptr);
        }
Exemplo n.º 5
0
        public NodeLL ReverseKPairs(NodeLL head, int k)
        {
            int    count   = 0;
            NodeLL current = head;
            NodeLL next    = null;
            NodeLL prev    = null;

            while (current != null && count < k)
            {
                next         = current.next;
                current.next = prev;
                prev         = current;
                current      = next;
                count++;
            }
            if (next != null)
            {
                head.next = ReverseKPairs(next, k);
            }
            return(prev);
        }
Exemplo n.º 6
0
        public NodeLL Merge(NodeLL l1, NodeLL l2)
        {
            NodeLL ptr  = new NodeLL(0);
            NodeLL head = ptr;

            while (l1 != null && l2 != null)
            {
                if (l1.data < l2.data)
                {
                    ptr.next = new NodeLL(l1.data);
                    l1       = l1.next;
                }
                else
                {
                    ptr.next = new NodeLL(l2.data);
                    l2       = l2.next;
                }
                ptr = ptr.next;
            }
            ptr.next = l1 != null ? l1 : l2;
            return(head.next);
        }
Exemplo n.º 7
0
        public NodeLL Merge(NodeLL ptr, NodeLL l1)
        {
            NodeLL res  = new NodeLL(0);
            NodeLL head = res;

            while (ptr != null && l1 != null)
            {
                if (ptr.data < l1.data)
                {
                    res = new NodeLL(ptr.data);
                    ptr = ptr.next;
                }
                else
                {
                    res = new NodeLL(l1.data);
                    l1  = l1.next;
                }
                res = res.next;
            }
            res.next = l1 != null ? l1 : ptr;
            return(head.next);
        }
Exemplo n.º 8
0
        public NodeLL SwapPairs(NodeLL head)
        {
            NodeLL node = head;

            head = node.next;
            NodeLL prev = null;

            while (head != null)
            {
                NodeLL nextPair = null;
                if (node.next != null)
                {
                    nextPair = node.next.next;
                    node     = Swap(head);
                }
                if (prev != null)
                {
                    prev.next = node;
                }
                prev = node.next;
                node = nextPair;
            }
            return(head);
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            //string s = "babad";
            //MS1 m = new MS1();
            //string res = m.longestPalindrome(s);

            // string s = "-42";
            // MS2 m = new MS2();
            //long res= m.SToI(s);

            //string s = "VIII";
            // MS3 n = new MS3();
            //n.RomanToInt(s);

            //MS4 m = new MS4();
            //int[] arr = { -1, 0, 1, 2, -1, -4 };
            //List<List<int>> l=m.ThreeSum(arr);

            //SortColors s = new SortColors();
            //int[] arr = { 2, 0, 2, 1, 1, 0 };
            //int []arr1=s.Sort(arr);

            //validParenthesis v = new validParenthesis();
            //string s = "{ ]";
            //bool res=v.IsValid1(s);

            NodeLL n = new NodeLL(1);

            n.next                = new NodeLL(2);
            n.next.next           = new NodeLL(3);
            n.next.next.next      = new NodeLL(4);
            n.next.next.next.next = new NodeLL(5);
            //NodeLL m = new NodeLL(1);
            //m.next = new NodeLL(3);
            //m.next.next = new NodeLL(4);

            //NodeLL[] l = { n, m };
            //MS6 p = new MS6();

            //MS7 m = new MS7();
            //NodeLL l=m.SwapPairs(n);

            //MS8 m = new MS8();
            //NodeLL res=m.ReverseKPairs(n, 2);

            //MS9 m1 = new MS9();
            //int[] arr = { 0, 0, 1, 1, 1, 2, 2, 3, 3, 4 };
            //int l = m1.RemoveDuplicates(arr);

            //string hay = "hello";
            //string needle = "ll";
            //MS10 m = new MS10();
            //int res=m.StrStr(hay, needle);

            //MS11 m = new MS11();
            //int[] arr = { 4, 5, 1, 2, 3 };
            //int res=m.Search(arr, 3);


            //MS12 p = new MS12();
            //int[] arr = { 1, 1, 3 };
            //List<List<int>> p2=p.Permutation(arr);

            //MS13 m = new MS13();
            //string s = "my name is parvathi";
            //string res=m.Rotate(s);

            //Trie o = new Trie();
            //string s = "leet";
            //o.Insert(s);
            //bool res1 = o.Search(s);
            //bool res2 = o.StartsWith("lee");

            char[,] board = { { 'o', 'a', 'a', 'n' }, { 'e', 't', 'a', 'e' },
                              { 'i', 'h', 'k', 'r' },
                              { 'i', 'f', 'l', 'v' } };
            string[]       words = { "oath", "pea", "eat", "rain" };
            TrieWordSearch t     = new TrieWordSearch();
            List <string>  res   = t.FindWords(board, words);
        }