C# 把数字转换成链表

例如:123456转换成 1 -> 2 -> 3-> 4-> 5-> 6

View Code
        static LinkedList<int> CovertIntToLinkedList(int num)
        {
            Stack<int> stack = new Stack<int>();
            LinkedList<int> result = new LinkedList<int>();
            while (num!=0)
            {
                stack.Push(num % 10);
                num = num / 10;
            }
            while (stack.Count>0)
            {
                result.AddLast(stack.Pop());
            }
            return result;
        }

        static LinkedList<int> CovertIntToLinkedList2(int num)
        {
            LinkedList<int> result = new LinkedList<int>();
            int[] intArr = new int[num.ToString().Length];
            int i = 0;
            while (num!=0)
            {
                intArr[i] = num% 10;
                num /=10;
                i++;
            }
            for (int j = intArr.Length-1; j >=0; j--)
            {
                result.AddLast(intArr[j]);
            }
            return result;
        }

 

posted @ 2012-11-09 15:45  Ligeance  阅读(373)  评论(0编辑  收藏  举报