Example #1
0
        public static ListNode GetKthLastNodeOfTheList(ListNode linkedList, int k)
        {
            int currentNodeIndex = 0;

            // Store list head pointer
            ListNode kthNode = linkedList;

            // Corner case
            if (linkedList == null) {
                return null;
            }

            // Skip first k nodes
            while ((currentNodeIndex < k) && (linkedList != null)) {
                linkedList = linkedList.GetNextNode();
                currentNodeIndex ++;
            }

            // No more nodes left in the list
            if (linkedList == null) {
                return null;
            }

            // Continue to iterate both pointers to the end of the list
            while (linkedList != null) {
                kthNode = kthNode.GetNextNode ();
                linkedList = linkedList.GetNextNode ();
            }

            return kthNode;
        }
Example #2
0
        public void Setup()
        {
            for (int i = 10; i >= 0; i--) {
                listHead = new ListNode (i, listHead);
            }

            listHead.Print ();
        }
Example #3
0
 public static ListNode GetFifthLastNodeOfTheList(ListNode linkedList)
 {
     // Return 5th last node from the end of the list
     return ListManager.GetKthLastNodeOfTheList (linkedList, 5);
 }
Example #4
0
 public ListNode(int data, ListNode nextNode)
 {
     this.data     = data;
     this.nextNode = nextNode;
 }
Example #5
0
 public ListNode()
 {
     this.data     = 0;
     this.nextNode = null;
 }
Example #6
0
 public ListNode(int data, ListNode nextNode)
 {
     this.data = data;
     this.nextNode = nextNode;
 }
Example #7
0
 public ListNode()
 {
     this.data = 0;
     this.nextNode = null;
 }
Example #8
0
 public void TearDown()
 {
     listHead = null;
 }