Robust code: linked list of nodes penultimate k

Title Description

Input a linked list, the linked list output reciprocal k-th node. For example: Enter a linked list, the linked list output reciprocal k-th node. In order to meet the habit of most people, this question counted from 1, that is the end node of the list is the penultimate one node. For example, a linked list has six nodes, the nodes start from scratch value thereof is successively 1,2,3,4,5,6. 3 penultimate node of this list is the value of the node 4.

Problem-solving ideas

Unusual solution, the length of the list to find n, k nodes found penultimate position n-k + 1 through the length of, found after traversing a result, the efficiency of this method is 2n, we do not use this method here, a method where a double pointer achieve.
1. The first pointer from the pointer list head traversing k-1 nodes, the second pointer does not move
2. Starting k steps, beginning from the second pointer is the head pointer list traversing
3. Since two pointer remains k-1, when the first pointer reaches the end of the list, just to reach the second pointer is also the penultimate node k.

Robustness: Robust Robust is a transliteration, which is robust and strong meaning, it is critical under abnormal and dangerous situation the system to survive. For example, computer software in the input errors, disk failures, network overload or intentional assault case, can not crash, do not crash, is the robustness of the software.

This robustness problem has three:
1. empty node
2 is smaller than the total number of nodes K
3.K less than 0

Code

/// <summary>
    /// 链表
    /// </summary>
    public class ListNode
    {
        public int item;
        public ListNode next;
        public ListNode(int x)
        {
            item = x;
        }

        /// <summary>
        /// 生成链表
        /// </summary>
        /// <param name="length"></param>
        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;

            //下一个
            ListNode nextList(ListNode node, int value)
            {
                while (node.next != null)
                {
                    node = node.next;
                }
                var next = new ListNode(value);
                node.next = next;
                return next;
            }
        }
    }
View Code
        public static ListNode FindBackKth(ListNode node, int k) {
            if (node == null || k <= 0) {
                return null;
            }

            ListNode firstNode = node;
            ListNode secondNode = node;
            for (int i = 1; i <= k - 1; i++) {
                firstNode = firstNode.next;
                if (firstNode == null) {
                    return null;
                }
            }

            while (firstNode.next != null) {
                firstNode = firstNode.next;
                secondNode = secondNode.next;
            }

            return secondNode;
        }

test

[Fact]
        public void TestNull()
        {
            ListNode listNode = null;
            Assert.Null(Coding014.FindBackKth(listNode,9));
        }

        [Fact]
        public void TestK()
        {
            ListNode listNode = new ListNode(1);
            Assert.Null(Coding014.FindBackKth(listNode, 0));
            Assert.Equal(1, Coding014.FindBackKth(listNode, 1).item);
            Assert.Null(Coding014.FindBackKth(listNode, 2));
        }

        [Fact]
        public void Test1()
        {
            //0,1,2,3,4,5
            ListNode listNode = ListNode.CreateNodeList(6);
            Assert.Null(Coding014.FindBackKth(listNode, 0));
            Assert.Null(Coding014.FindBackKth(listNode, 7));
            Assert.Equal(3, Coding014.FindBackKth(listNode, 3).item);
            Assert.Equal(5, Coding014.FindBackKth(listNode, 1).item);
            Assert.Equal(0, Coding014.FindBackKth(listNode, 6).item);
        }
View Code

 

Guess you like

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