[Print head face questions from the end of the list is inscribed [Solution] exquisite string replace spaces 002] [sophisticated solution is inscribed in the two-dimensional array 001 to find subtle Solution]

Title Description

Enter a list, List Value Returns a list by the order from the head to the tail.

Problem-solving ideas

Enter a list, to the head from the tail output, the normal traverse are output from beginning to end, and here need the head from the tail output, is the "last out", that is, the stack function.

Code

Stack manner

public static List<int> PrintForStack(ListNode nodes)
{
    Stack<ListNode> listNodes = new Stack<ListNode>();
    ListNode node = nodes;
    while (node != null)
    {
        listNodes.Push(node);
        node = node.next;
    }

    List<int> list = new List<int>();
    foreach (var item in listNodes) {
        list.Add(item.item);
    }
    return list;
}

Recursive manner

public static List<int> PrintForRecursion(ListNode node) {
    List<int> listNodes = new List<int>();
    PrintForRecursion(node, listNodes);
    return listNodes;
}

private static void PrintForRecursion(ListNode node, List<int> list)
{
    if (node != null) {
        if (node.next != null) {
            PrintForRecursion(node.next, list);
        }
        list.Add(node.item);
    }
}

Head in the clouds: expansion of thinking, imagination

1. Enter a list, returns a list upside down

public static ListNode PrintForReverse(ListNode nodes)
{
    Stack<ListNode> listNodes = new Stack<ListNode>();
    ListNode node = nodes;
    while (node != null)
    {
        listNodes.Push(node);
        node = node.next;
    }

    ListNode reverseNode = listNodes.Pop();
    var temp = reverseNode;
    foreach (var item in listNodes)
    {
        item.next = null;
        temp.next = item;
        temp = item;
    }
    return reverseNode;
}

2. Generate a list

public static ListNode CreateNodeList(int length)
{
    ListNode listNode = new ListNode(0);
    var temp = listNode;
    for (int i = 1; i < length; i++)
    {
        temp = nextList(temp, i);
    }

    return listNode;

    // next 
    ListNode nextList (ListNode the Node, int value)
    {
        while (node.next != null)
        {
            node = node.next;
        }
        var next = new ListNode(value);
        node.next = next;
        return next;
    }
}

test

[Fact]
public void TestList()
{
    ListNode listNode = Coding003.CreateNodeList(10);
    List<int> test = Coding003.PrintForStack(listNode);
    for (int i = 0; i < 10; i++)
    {
        Assert.Equal(i, test[9 - i]);
    }

    List<int> test2 = Coding003.PrintForRecursion(listNode);
    for (int i = 0; i < 10; i++)
    {
        Assert.Equal(i, test2[9 - i]);
    }
}

[Fact]
public void Test()
{
    ListNode listNode = Coding003.CreateNodeList(10);
    string o = JsonConvert.SerializeObject(listNode);

    List<int> test = Coding003.PrintForStack(listNode);
    string t = JsonConvert.SerializeObject(test);

    List<int> test2 = Coding003.PrintForRecursion(listNode);
    string t2 = JsonConvert.SerializeObject(test2);

    ListNode test3 = Coding003.PrintForReverse(Coding003.PrintForReverse(listNode));
    string t3 = JsonConvert.SerializeObject(test3);

    Assert.Equal(test, test2);
    Assert.Equal(o, t3);
}
View Code

Interview questions string [002] Alternatively spaces subtle Solution

Find face questions [001] two-dimensional array of exquisite Solution

Guess you like

Origin www.cnblogs.com/zhao123/p/11133953.html