leetcode 面試題22. 鏈表中倒數第k個節點

 

 

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution 
{
    public ListNode GetKthFromEnd(ListNode head, int k) 
    {
        ListNode res = head;
        int index = 1;
        while(true)
        {
            head = head.next;
            if(index > k)
                res = res.next;
            index++;
            if(head == null)
                return res;
        }
    }
}

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章