Monday, November 23, 2015

Kth to Last Element in Linked List

The Linked List program is available here: link
    public String kthLastElement(int k)    {
        LinkedList L = this;
        for(int i=1; i<k; i++)    {
            L=L.next;
        }
        StringBuffer sb = new StringBuffer();
        while(L!=null)    {
            sb.append(L.data).append(", ");
            L=L.next;
        }
        return sb.toString();
    }

No comments:

Post a Comment

UA-39217154-2