예제 #1
0
        public static void removeDuplicate(LinkedNode head)
        {
            LinkedNode previous = head;
            LinkedNode current = head.Next;
            LinkedNode runner = head;
            while (current != null)
            {
                while (runner != current)
                {
                    if (runner.Data.Equals(current.Data))
                    {
                        //never change link, copy the value
                        double temp = current.Next.Data;
                        current.Data = temp;
                        current.Next = current.Next.Next;
                        break;
                    }
                    runner = runner.Next;
                }

                if (runner == current)
                {
                    previous = current;
                    current = current.Next;
                    runner = head;
                }
            }
        }
예제 #2
0
        private static Result verifyPalindrome(LinkedNode head, int length)
        {
            if (length == 2)
            {
                Result r = new Result(head.Next.Next, head.Data == head.Next.Data);
                return r;
            }
            else if (length == 1)
            {
                Result r = new Result(head.Next, true);
                return r;
            }
            else if (length == 0)
            {
                return new Result(null, true);
            }

            Result result = verifyPalindrome(head.Next, length - 2);

            if (result == null)
            {
                return result;
            }
            else
            {
                result.quilify = result.node.Data == head.Data;
                result.node = result.node.Next;
                return result;

            }
        }
예제 #3
0
 public LinkedNode Insert(LinkedNode Other)
 {
     if (Other.Weight <= this.Weight)
     {
         if (this.Next != null)
         {
             this.Next.Prev = Other;
             Other.Next = this.Next;
         }
         this.Next = Other;
         Other.Prev = this;
         return Other;
     }
     if (this.Prev == null)
     {
         Other.Prev = null;
         this.Prev = Other;
         Other.Next = this;
     }
     else
     {
         this.Prev.Insert(Other);
     }
     return this;
 }
예제 #4
0
        //Math: 1step runs, 2steps, finally they meet at the point at from k step from loop, k is start distance
        public static LinkedNode findLoopNode(LinkedNode head)
        {
            LinkedNode p1 = head;
            LinkedNode p2 = head;
            while (true)
            {
                p1 = p1.Next;
                p2 = p2.Next.Next;
                if (p1 == p2)
                {
                    break;
                }
            }

            if (p1 == null || p2==null)
            {
                return null;
            }

            p1 = head;
            while (true)
            {
                if (p1 == p2)
                {
                    return p1;
                }
                p1 = p1.Next;
                p2 = p2.Next;
            }
        }
예제 #5
0
파일: P8_5.cs 프로젝트: slao-learn/epi
 public static LinkedNode CommonNode(LinkedNode a, LinkedNode b)
 {
     int aLength = 0, bLength = 0;
     LinkedNode aIter = a, bIter = b;
     while (aIter != null)
     {
         aLength++;
         aIter = aIter.next;
     }
     while (bIter != null)
     {
         bLength++;
         bIter = bIter.next;
     }
     if (aLength < bLength)
     {
         aIter = b;
         bIter = a;
     } else
     {
         aIter = a;
         bIter = b;
     }
     for (int i = 0; i < Math.Abs(aLength - bLength); ++i)
     {
         aIter = aIter.next;
     }
     while (aIter != bIter && aIter != null && bIter != null)
     {
         aIter = aIter.next;
         bIter = bIter.next;
     }
     return aIter;
 }
예제 #6
0
 public static bool verifyPalindromeIterate(LinkedNode head)
 {
     System.Collections.Generic.Stack<double> stack = new System.Collections.Generic.Stack<double>();
     LinkedNode SlowRunner = head;
     LinkedNode FastRunner = head;
     while (FastRunner != null && FastRunner.Next != null)
     {
         stack.Push(SlowRunner.Data);
         FastRunner = FastRunner.Next.Next;
         SlowRunner = SlowRunner.Next;
     }
     SlowRunner = SlowRunner.Next;
     while (SlowRunner!=null)
     {
         if (stack.Pop() == SlowRunner.Data)
         {
             SlowRunner = SlowRunner.Next;
         }
         else
         {
             return false;
         }
     }
     return true;
 }
예제 #7
0
파일: P8_1.cs 프로젝트: slao-learn/epi
    private static LinkedNode Merge(LinkedNode l1, LinkedNode l2)
    {
        LinkedNode head = null;
        LinkedNode l1p = l1, l2p = l2;
        if (l1.data < l2.data)
        {
            head = l1;
            l1p = l1.next;
        } else
        {
            head = l2;
            l2p = l2.next;
        }
        LinkedNode cur = head;

        while (l1p != null && l2p != null)
        {
            if (l1p.data < l2p.data)
            {
                cur.next = l1p;
                l1p = l1p.next;
            } else
            {
                cur.next = l2p;
                l2p = l2p.next;
            }
            cur = cur.next;
        }
        cur.next = (l1p != null ? l1p : l2p);
        return head;
    }
예제 #8
0
        //Sample
        public static LinkedNode addLists(LinkedNode l1, LinkedNode l2, int carry)
        {
            if (l1 == null && l2 == null && carry == 0)
            {
                return null;
            }

            LinkedNode result = new LinkedNode(carry);

            double value = carry;
            if (l1 != null)
            {
                value += l1.Data;
            }
            if (l2 != null)
            {
                value += l2.Data;
            }
            result.Data = value % 10;
            if (l1 != null || l2 != null || value >= 10)
            {
                LinkedNode more = addLists(
                    l1 == null ? null : l1.Next,
                    l2 == null ? null : l2.Next,
                    value >= 10 ? 1 : 0
                    );
                result.Next = more;
            }
            return result;
        }
예제 #9
0
        public string Pop()
        {
            var result = tail.Data;

            tail = tail.Previous;
            Count--;
            return(result);
        }
    public void removeNode(LinkedNode node)
    {
        LinkedNode next = node.next;
        LinkedNode prev = node.prev;

        prev.next = next;
        next.prev = prev;
    }
예제 #11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="node"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        protected LinkedNode <T> InsertBefore(LinkedNode <T> node, ref T value)
        {
            LinkedNode <T> result = LinkedNode <T> .InsertBefore(node, ref value);

            System.Threading.Interlocked.Increment(ref m_Count);

            return(result);
        }
예제 #12
0
        protected LinkedNode <T> AddAfter(LinkedNode <T> node, ref T value)
        {
            LinkedNode <T> result = LinkedNode <T> .AddAfter(node, ref value);

            System.Threading.Interlocked.Increment(ref m_Count);

            return(result);
        }
예제 #13
0
        public static LinkedNode <T> Prob_2_7_DoListsIntersect_v1 <T>(LinkedNode <T> firstList, LinkedNode <T> secondList)
        {
            var runner         = firstList;
            var firsListLength = 1;

            while (runner.nextNode != null)
            {
                runner = runner.nextNode;
                firsListLength++;
            }

            var secondRunner     = secondList;
            var secondListLength = 1;

            while (secondRunner.nextNode != null)
            {
                secondRunner = secondRunner.nextNode;
                secondListLength++;
            }

            //If nodes do not have the same last node
            // they must not intersect
            if (runner != secondRunner)
            {
                return(null);
            }

            var offset = firsListLength - secondListLength;

            offset = offset < 0 ? (offset * -1) : offset;

            //reset runners
            runner       = firstList;
            secondRunner = secondList;
            if (firsListLength > secondListLength)
            {
                runner       = firstList.GetNodeXPositionsFromCurrent(offset);
                secondRunner = secondList;
            }
            else if (secondListLength > firsListLength)
            {
                runner       = firstList;
                secondRunner = secondList.GetNodeXPositionsFromCurrent(offset);
            }

            while (runner.nextNode != null || secondRunner.nextNode != null)
            {
                if (runner == secondRunner)
                {
                    return(runner);
                }

                runner       = runner.nextNode;
                secondRunner = secondRunner.nextNode;
            }

            return(null);
        }
예제 #14
0
        private static void RotateLeft(CustomLinkedList <int> linkedList, int position)
        {
            if (position == 0)
            {
                return;
            }

            // Let us understand the below
            // code for example k = 4
            // and list = 10->20->30->40->50->60.
            LinkedNode <int> current = linkedList.Head;

            // current will either point to kth
            // or NULL after this loop. current
            // will point to node 40 in the above example
            int count = 1;

            while (count < position && current.Next != null)
            {
                current = current.Next;
                count++;
            }

            // If current is NULL, k is greater than
            // or equal to count of nodes in linked list.
            // Don't change the list in this case
            if (current == null)
            {
                return;
            }

            // current points to kth node.
            // Store it in a variable.
            // kthNode points to node
            // 40 in the above example
            LinkedNode <int> kthNode = current;

            // current will point to
            // last node after this loop
            // current will point to
            // node 60 in the above example
            while (current.Next != null)
            {
                current = current.Next;
            }

            // Change next of last node to previous head
            // Next of 60 is now changed to node 10

            current.Next = linkedList.Head;

            // Change head to (k+1)th node
            // head is now changed to node 50
            linkedList.Head = kthNode.Next;

            // change next of kth node to null
            kthNode.Next = null;
        }
 public LRUCache(int capacity)
 {
     this.size     = 0;
     this.capacity = capacity;
     head          = new LinkedNode();
     tail          = new LinkedNode();
     head.next     = tail;
     tail.prev     = head;
 }
    //always add new node right after the head
    public void addNode(LinkedNode node)
    {
        LinkedNode temp = head.next;

        head.next = node;
        node.prev = head;
        node.next = temp;
        temp.prev = node;
    }
예제 #17
0
 public ContainerRegistration(LinkedNode <Type, object> validators, Type mappedTo, LifetimeManager lifetimeManager, InjectionMember[] injectionMembers = null)
 {
     Type  = mappedTo;
     Key   = typeof(LifetimeManager);
     Value = lifetimeManager;
     LifetimeManager.InUse = true;
     InjectionMembers      = injectionMembers;
     Next = validators;
 }
예제 #18
0
 public static void deleteNode(LinkedNode ln)
 {
     if (ln.Next == null || ln == null)
     {
         return;
     }
     ln.Data = ln.Next.Data;
     ln.Next = ln.Next.Next;
 }
        /// <summary>
        /// <para>Expected Runtime: 0(1)</para>
        /// </summary>
        /// <param name="value"></param>
        private void ReplaceMinimumNode(T value)
        {
            LinkedNode <T> newMinimumNode = new LinkedNode <T>(value);

            this.isCachedMinimumValid = false;

            newMinimumNode.InsertBetween(null, this.headNodeOfList);
            this.headNodeOfList = newMinimumNode;
        }
예제 #20
0
        public void addFirst(T item)
        {
            LinkedNode temp = new LinkedNode();

            temp.value = item;
            temp.next  = head.next;
            count++;
            head.next = temp;
        }
예제 #21
0
        private void removeNode(LinkedNode node)
        {
            LinkedNode before = node.back;
            LinkedNode after  = node.next;

            before.next = after;
            after.back  = before;
            SIZE--;
        }
예제 #22
0
        public int Count()
        {
            if (LinkedNode == null)
            {
                return(1);
            }

            return(1 + LinkedNode.Count());
        }
예제 #23
0
        public List <int> Go()
        {
            SinglyLinkedListImpl imp  = new SinglyLinkedListImpl();
            ILinkedNode <int>    root = new LinkedNode <int>();

            imp.FillLinkedList(root);
            Sort(root);
            return(new List <int>());
        }
예제 #24
0
        /// <summary>
        /// <para>Push <param name="value">value</param> onto the stack.</para>
        /// <para>Expected Runtime: O(1)</para>
        /// </summary>
        public void Push(T value)
        {
            LinkedNode <T> newNode = new LinkedNode <T>(value);

            newNode.Next         = this.headNodeOfStack;
            this.headNodeOfStack = newNode;

            this.Count++;
        }
예제 #25
0
파일: P8_8.cs 프로젝트: slao-learn/epi
 public static void RunTests()
 {
     LinkedNode l = new LinkedNode(1, new LinkedNode(2, new LinkedNode(3, new LinkedNode(4, null))));
     Console.WriteLine(Print(l));
     Console.WriteLine(DeleteKth(l, 2).data);
     Console.WriteLine(Print(l));
     Console.WriteLine(DeleteKth(l, 1).data);
     Console.WriteLine(Print(l));
 }
예제 #26
0
 public static void deleteNode(LinkedNode ln)
 {
     if (ln.Next==null || ln ==null)
     {
         return;
     }
     ln.Data = ln.Next.Data;
     ln.Next = ln.Next.Next;
 }
예제 #27
0
        public void InsertBetween(LinkedNode <T> previous, LinkedNode <T> next)
        {
            if (previous != null)
            {
                previous.Next = this;
            }

            this.Next = next;
        }
예제 #28
0
 private LinkedNode <T> GetByIndex(LinkedNode <T> temp, int index)
 {
     result = temp;
     if (index != 0)                                 // go to the next until index >0
     {
         GetByIndex(temp.Next, index - 1);
     }
     return(result);
 }
예제 #29
0
        public static LinkedNode <int> Prob_2_4_Partition_v1(LinkedNode <int> head, int partitionNum)
        {
            LinkedNode <int> prePivot = null;
            var runner = head;

            //Needed if first node is equal or grater than partition.
            //We swipe the first node smaller to the parititon to the head.
            if (runner.data >= partitionNum)
            {
                Console.WriteLine($"First node {runner.data} is equal or greater than partition {partitionNum}");
                while (runner.nextNode != null && prePivot == null)
                {
                    Console.WriteLine($"Comparing next node {runner.nextNode.data} to {partitionNum}");
                    //If we find a smaller node, we set it as the prepivot
                    if (runner.nextNode.data < partitionNum)
                    {
                        var nodeToMoveBack = runner.nextNode;
                        Console.WriteLine($"Next node {nodeToMoveBack.data} is smaller than parition. Moving");
                        runner.nextNode = runner.nextNode.nextNode;

                        nodeToMoveBack.nextNode = head;
                        head     = nodeToMoveBack;
                        prePivot = nodeToMoveBack;

                        Console.WriteLine($"New head {head.data}");
                    }
                    else
                    {
                        runner = runner.nextNode;
                    }
                }
            }

            //Continue with runner
            while (runner.nextNode != null)
            {
                if (prePivot == null && runner.nextNode.data >= partitionNum)
                {
                    prePivot = runner;
                }
                else if (prePivot != null && runner.nextNode.data < partitionNum)
                {
                    var nodeToMoveBack = runner.nextNode;
                    runner.nextNode = runner.nextNode.nextNode;

                    nodeToMoveBack.nextNode = prePivot.nextNode;
                    prePivot.nextNode       = nodeToMoveBack;
                }
                else
                {
                    runner = runner.nextNode;
                }
            }

            return(head);
        }
예제 #30
0
        static void Main(string[] args)
        {
            LinkedListGenerator llg         = new LinkedListGenerator();
            List <int>          _linkedList = new List <int>();
            LinkedNode          linkNode    = llg.OneWayLiked(10, ref _linkedList);

            Metods _metods = new Metods();

            _metods.ReverseList(linkNode);
        }
예제 #31
0
파일: P8_2.cs 프로젝트: slao-learn/epi
 private static string Print(LinkedNode p)
 {
     string o = p.data.ToString();
     while (p.next != null)
     {
         o += " -> " + p.next.data;
         p = p.next;
     }
     return o;
 }
예제 #32
0
    public static void RunTests()
    {
        LinkedNode l = new LinkedNode(1, new LinkedNode(2, new LinkedNode(3, new LinkedNode(4, null))));

        Console.WriteLine(Print(l));
        Console.WriteLine(DeleteKth(l, 2).data);
        Console.WriteLine(Print(l));
        Console.WriteLine(DeleteKth(l, 1).data);
        Console.WriteLine(Print(l));
    }
예제 #33
0
        public void ListNodes(LinkedNode head, ref List <int> list)
        {
            if (head.next == null)
            {
                return;
            }

            list.Add(head.val);
            ListNodes(head.next, ref list);
        }
예제 #34
0
        public static void Prob_2_3_DeleteMiddleNode_v1 <T>(LinkedNode <T> middleNode)
        {
            var nextNode = middleNode.nextNode;

            if (nextNode != null)
            {
                middleNode.data     = nextNode.data;
                middleNode.nextNode = nextNode.nextNode;
            }
        }
예제 #35
0
    public static void Output()
    {
        LinkedNode node = myList.getFirst();

        while (node != null)
        {
            Console.WriteLine("" + node.n);
            node = node.next;
        }
    }
예제 #36
0
        /// <summary>
        /// Implementation of ILinkedeNodeConfig
        /// </summary>
        /// <param name="linkedNode">The linked node</param>
        /// <returns>The created factory</returns>
        BaseNodeFactory ILinkedNodeConfig.CreateFactory(BaseNodeFactory linkedNode)
        {
            NetGraphContainerNodeFactory ret  = (NetGraphContainerNodeFactory)LinkedNode.CreateFactory();
            NetGraphContainerNodeFactory link = (NetGraphContainerNodeFactory)linkedNode;

            ret.LinkedNode  = link;
            link.LinkedNode = ret;

            return(ret);
        }
예제 #37
0
            public void DisplayAll()
            {
                LinkedNode temp = Num;

                do
                {
                    Console.WriteLine(temp.Number);
                    temp = temp.Next;
                } while (temp != null);
            }
예제 #38
0
        /// <summary>
        /// <para>Sets all cached values to null and all booleans related
        /// to count, min and max to false.</para>
        /// </summary>
        protected void InvalidateAllCaches()
        {
            this.isCachedMinimumValid = false;
            this.cachedMinimum        = null;

            this.isCachedMaximumValid = false;
            this.cachedMaximum        = null;

            this.isCachedCountValid = false;
        }
예제 #39
0
            public static LinkedNode AddFirst(int value)
            {
                LinkedNode ln = new LinkedNode();

                ln.Value    = value;
                ln.Previous = ln;
                ln.Next     = ln;

                return(ln);
            }
예제 #40
0
        private LinkedNode nodeAt(int index)
        {
            LinkedNode node = first;

            for (int i = -1; i < index; i++)
            {
                node = node.next;
            }
            return(node);
        }
예제 #41
0
 public void printList()
 {
     Console.Write("List ");
     while (this.head != null)
     {
         Console.Write("{0} ", this.head.value);
         this.head = this.head.next;
     }
     Console.WriteLine("");
 }
예제 #42
0
파일: P8_5.cs 프로젝트: slao-learn/epi
    public static void RunTests()
    {
        LinkedNode tail = new LinkedNode(6, new LinkedNode(7, new LinkedNode(8, new LinkedNode(9, null))));
        LinkedNode a = new LinkedNode(1, new LinkedNode(3, new LinkedNode(10, tail)));
        LinkedNode b = new LinkedNode(2, new LinkedNode(4, new LinkedNode(5, tail)));
        Console.WriteLine(Print(CommonNode(a, b)));

        a = new LinkedNode(1, new LinkedNode(3, new LinkedNode(10, null)));
        b = new LinkedNode(2, new LinkedNode(4, new LinkedNode(5, null)));
        Console.WriteLine(Print(CommonNode(a, b)));
    }
예제 #43
0
파일: P8_2.cs 프로젝트: slao-learn/epi
 public static void RunTests()
 {
     LinkedNode a = new LinkedNode(3, new LinkedNode(5, new LinkedNode(7, null)));
     Console.WriteLine("a = " + Print(a));
     LinkedNode b = new LinkedNode(1, new LinkedNode(2, new LinkedNode(5, new LinkedNode(8, null))));
     Console.WriteLine("b = " + Print(b));
     LinkedNode c = Reverse(a);
     Console.WriteLine("c = " + Print(c));
     c = Reverse(b);
     Console.WriteLine("c = " + Print(c));
     Console.WriteLine("c = " + Print(Reverse(new LinkedNode(4, null))));
 }
예제 #44
0
 public static LinkedNode getInst()
 {
     LinkedNode l1 = new LinkedNode(1);
     LinkedNode l2 = new LinkedNode(2);
     LinkedNode l3 = new LinkedNode(3);
     LinkedNode l4 = new LinkedNode(4);
     LinkedNode l5 = new LinkedNode(5);
     l1.Next = l2;
     l2.Next = l3;
     l3.Next = l4;
     l4.Next = l5;
     return l1;
 }
예제 #45
0
 public static void reverse(LinkedNode start_node)
 {
     LinkedNode current_node = start_node;
     LinkedNode last_node = null;
     LinkedNode next_node = start_node.Next;
     while (next_node != null)
     {
         current_node.Next = last_node;
         last_node = current_node;
         current_node = next_node;
         next_node = next_node.Next;
     }
 }
예제 #46
0
        public static bool verifyPalindrome(LinkedNode head)
        {
            int length = 0;
            LinkedNode loop = head;
            while (loop != null)
            {
                loop = loop.Next;
                length++;
            }

            Result r = verifyPalindrome(head, length);

            return r.quilify;
        }
예제 #47
0
        public static int getNthToLastRecursiveEx2(LinkedNode head, int n)
        {
            if (head == null)
            {
                return 0;
            }
            int i = getNthToLastRecursiveEx2(head.Next, n) + 1;

            if (i == n)
            {
                result = head;
            }

            return i;
        }
예제 #48
0
 public static LinkedNode getNthToLastIterate(LinkedNode head, int n)
 {
     LinkedNode ToTarget = head;
     LinkedNode ToEnd = head;
     for (int i = 0; i < n; i++)
     {
         ToEnd = ToEnd.Next;
     }
     while (ToEnd!=null)
     {
         ToTarget = ToTarget.Next;
         ToEnd = ToEnd.Next;
     }
     return ToTarget;
 }
예제 #49
0
 public static void reverseRecursive(LinkedNode current_node, LinkedNode next_node = null, LinkedNode last_node = null)
 {
     if (next_node == null)
     {
         if (last_node == null)
         {
             next_node = current_node.Next;
         }
         else
         {
             return;
         }
     }
     current_node.Next = last_node;
     reverseRecursive(next_node, next_node.Next, current_node);
 }
예제 #50
0
파일: P8_1.cs 프로젝트: slao-learn/epi
    public static void RunTests()
    {
        LinkedNode a = new LinkedNode(3, new LinkedNode(5, new LinkedNode(7, null)));
        Console.WriteLine("a = " + Print(a));
        LinkedNode b = new LinkedNode(1, new LinkedNode(2, new LinkedNode(5, new LinkedNode(8, null))));
        Console.WriteLine("b = " + Print(b));
        LinkedNode c = Merge(a, b);
        Console.WriteLine("c = " + Print(c));

        DoublyLinkedNode d = new DoublyLinkedNode(3, new DoublyLinkedNode(5, new DoublyLinkedNode(7, null)));
        Console.WriteLine("d = " + Print(d));
        DoublyLinkedNode e = new DoublyLinkedNode(1, new DoublyLinkedNode(2, new DoublyLinkedNode(5, new DoublyLinkedNode(8, null))));
        Console.WriteLine("e = " + Print(e));
        DoublyLinkedNode f = Merge(d, e);
        Console.WriteLine("f = " + Print(f));
    }
예제 #51
0
파일: P8_4.cs 프로젝트: slao-learn/epi
    public static void RunTests()
    {
        LinkedNode a = new LinkedNode(3, new LinkedNode(5, new LinkedNode(7, null)));
        Console.WriteLine("a = " + Print(a));
        Console.WriteLine(Print(TestCycle(a)));

        // 1 -> 2 -> 5 -> 8 -> 2 -> 5 -> ... looping
        LinkedNode d = new LinkedNode(2, null);
        LinkedNode c = new LinkedNode(8, null);
        d.next = new LinkedNode(5, c);
        LinkedNode b = new LinkedNode(1, d);
        c.next = d;
        Console.WriteLine(TestCycle(b).data);

        Console.WriteLine(Print(TestCycle(new LinkedNode(1, null))));
    }
예제 #52
0
        public static LinkedNode partitionLinkedlistEx(LinkedNode head, double x)
        {
            if (head == null)
            {
                return null;
            }
            LinkedNode PivotSmall = null, PivotLarge = null;
            LinkedNode loop = head, next =head.Next;
            while (next != null)
            {
                next = loop.Next;

                if (loop.Data <= x)
                {
                    if (PivotSmall == null)
                    {
                        loop.Next = null;
                        PivotSmall = loop;
                    }
                    else
                    {
                        loop.Next = PivotSmall;
                        PivotSmall = loop;
                    }
                }
                else
                {
                    if (PivotLarge == null)
                    {
                        loop.Next = null;
                        PivotLarge = loop;
                    }
                    else
                    {
                        loop.Next = PivotLarge;
                        PivotLarge = loop;
                    }
                }
                loop = next;
            }

            loop = PivotSmall;
            for (; loop.Next != null; loop = loop.Next) ;
            loop.Next = PivotLarge;

            return PivotSmall;
        }
예제 #53
0
        public static LinkedNode getNthToLastRecursive(LinkedNode head, int n)
        {
            if (head == null)
            {
                count = 0;
                return null;
            }

            LinkedNode node = getNthToLastRecursive(head.Next, n);
            count++;
            if (count == n)
            {
                return head;
            }

            return node;
        }
예제 #54
0
파일: P8_2.cs 프로젝트: slao-learn/epi
    private static LinkedNode Reverse(LinkedNode l)
    {
        if (l.next == null)
            return l;

        LinkedNode c = l.next, n = l.next.next, t = null;
        l.next = null;
        c.next = l;
        while (n != null)
        {
            t = n.next;
            n.next = c;
            c = n;
            n = t;
        }
        return c;
    }
예제 #55
0
파일: P8_11.cs 프로젝트: slao-learn/epi
        public static void RunTests()
        {
            LinkedNode l = new LinkedNode(0, new LinkedNode(1, new LinkedNode(2, new LinkedNode(3, new LinkedNode(4, null)))));
            EvenOddMerge(l);
            Console.WriteLine(Print(l));

            l = new LinkedNode(0, null);
            EvenOddMerge(l);
            Console.WriteLine(Print(l));

            l = new LinkedNode(0, new LinkedNode(1, null));
            EvenOddMerge(l);
            Console.WriteLine(Print(l));

            l = new LinkedNode(0, new LinkedNode(1, new LinkedNode(2, null)));
            EvenOddMerge(l);
            Console.WriteLine(Print(l));
        }
예제 #56
0
파일: P8_8.cs 프로젝트: slao-learn/epi
 public static LinkedNode DeleteKth(LinkedNode l, int k)
 {
     LinkedNode result = null;
     LinkedNode t = l;
     while (k >= 0 && t != null)
     {
         t = t.next;
         --k;
     }
     while (t != null)
     {
         t = t.next;
         l = l.next;
     }
     result = l.next;
     l.next = l.next.next;
     return result;
 }
예제 #57
0
        public static LinkedNode partitionLinkedlist(LinkedNode head, double x)
        {
            if (head == null)
            {
                return null;
            }
            LinkedNode PivotSmallStart = null, PivotSmallEnd = null, PivotLargeStart = null, PivotLargeEnd = null;
            LinkedNode loop = head;
            while (loop != null)
            {
                //note: you cannot interate from start
                if (loop.Data <= x)
                {
                    if (PivotSmallStart == null)
                    {
                        PivotSmallEnd = loop;
                        PivotSmallStart = PivotSmallEnd;
                    }
                    else
                    {
                        PivotSmallEnd.Next = loop;
                        PivotSmallEnd = loop;
                    }
                }
                else
                {
                    if (PivotLargeStart == null)
                    {
                        PivotLargeEnd = loop;
                        PivotLargeStart = PivotLargeEnd;
                    }
                    else
                    {
                        PivotLargeEnd.Next = loop;
                        PivotLargeEnd = loop;
                    }
                }
                loop = loop.Next;
            }

            PivotSmallEnd.Next = PivotLargeStart;
            PivotLargeEnd.Next = null;
            return PivotSmallStart;
        }
예제 #58
0
        public static LinkedNode addition(LinkedNode ln1, LinkedNode ln2)
        {
            double loop = 0;
            LinkedNode head = null, end = null;
            while (ln1 != null || ln2 != null)
            {

                double data1 = 0, data2 = 0;
                if (ln1 == null)
                {
                    data1 = 0;
                }
                else
                {
                    data1 = ln1.Data;
                    ln1 = ln1.Next;
                }
                if (ln2 == null)
                {
                    data2 = 0;
                }
                else
                {
                    data2 = ln2.Data;
                    ln2 = ln2.Next;
                }
                double data = (data1 + data2 + loop) % 10;
                loop = (int)(data1 + data2 + loop) / 10;

                if (head == null)
                {
                    head = new LinkedNode(data);
                    end = head;
                }
                else
                {
                    LinkedNode ln = new LinkedNode(data);
                    end.Next = ln;
                    end = ln;
                }
            }
            return head;
        }
예제 #59
0
파일: P8_11.cs 프로젝트: slao-learn/epi
        public static void EvenOddMerge(LinkedNode l)
        {
            LinkedNode last = l;
            while (last.next != null)
                last = last.next;
            LinkedNode t = last;
            LinkedNode h = l;

            while (h != last && h != null)
            {
                LinkedNode tmp = h.next;
                if (tmp.next != null)
                {
                    h.next = tmp.next;
                    t.next = tmp;
                    tmp.next = null;
                }
                t = t.next;
                h = h.next;
            }
        }
예제 #60
0
파일: P8_4.cs 프로젝트: slao-learn/epi
    private static LinkedNode TestCycle(LinkedNode l)
    {
        if (l.next == null)
            return null;

        LinkedNode a = l, b = l;
        while (b != null && b.next != null)
        {
            a = a.next;
            b = b.next.next;

            if (a == b)
            {
                b = l;
                while (a != b)
                {
                    a = a.next;
                    b = b.next;
                }
                return a;
            }
        }
        return null;
    }