plz help me out with the code here
thanks,
Ravna

Dani AI

Generated

As explained, "nth last" means last = 1, second last = 2, etc. 's snippet only handles the final node; for the general nth-from-end case in a single traversal use the two-pointer (fast/slow) technique that alluded to. The trick is to keep the fast pointer n nodes ahead of slow, so when fast reaches the list end, slow sits just before the node to remove.

Create a dummy node that points to the head (this cleanly handles deleting the actual head). Advance fast n+1 steps from the dummy, then move both fast and slow forward together until fast becomes NULL. slow->next is the node to delete. Validate n >= 1 and handle the case where n is larger than the list length (return unchanged or signal error).

struct Node {
    int data;
    struct Node *next;
};

struct Node* deleteNthFromEnd(struct Node *head, int n) {
    if (n <= 0) return head; /* invalid n */

    struct Node dummy;
    dummy.next = head;
    struct Node *fast = &dummy, *slow = &dummy;

    for (int i = 0; i <= n; ++i) {
        if (fast == NULL) return head; /* n > length */
        fast = fast->next;
    }

    while (fast != NULL) {
        fast = fast->next;
        slow = slow->next;
    }

    struct Node *to_delete = slow->next;
    if (to_delete) {
        slow->next = to_delete->next;
        free(to_delete);
    }
    return dummy.next;
}

Troubleshooting: segmentation faults usually mean n exceeded the length or the dummy/advance logic was omitted. Remember to return the new head (dummy.next) since the head can be removed. For a compact problem statement and variants, see LeetCode 19.

Recommended Answers

All 8 Replies

Use this algorithm to delete the last node.

void DelLast(List L)
{
	List Temp;
	if(L==NULL||L->Next==NULL)
	{
		printf("List contains no or just one element.");
		return;
	}
	while(L->Next->Next!=NULL)
		L=L->Next;
	Temp=L->Next;
	L->Next=NULL;
	free(Temp);
}

// Won't work for a single unit I guess.

However, am not sure what you mean by n'th last node. Isn't last ... just last?

>However, am not sure what you mean by n'th last node. Isn't last ... just last?
Last is just last, but nth isn't necessarily the last. If you're given n, you delete the node at that position. It's actually painfully easy to do with one traversal if you think about it for a few seconds. ravina_malik is just lazy and wants free homework solutions.

hey Narue Meanie,
when i say nth last node..i mean if u keep a counter on the last node & count backwards, its the nth node .. assuming the last node to b node no 1 ..count in opposite direction
hope this clarifies my being lazy & lets c how u do ur homework ....
Ravina

>when i say nth last node..i mean <snip my explanation in different words>
I know exactly what your requirements are. In fact, if you had been paying attention, you would have seen that I clarified QwertyManiac's confusion.

>hope this clarifies my being lazy
Your being lazy was very clear to begin with.

>& lets c how u do ur homework ....
I did my homework. That's why I make a very good living as a professional programmer. Why should I do your homework too?

hey ill rather not get into this..
end of the day i needed help
if u can priovide well & good if not...well plz ignore my post..
i prefer u not making assumptions on who or what i am...
ok???
lets keep it polite here

>lets keep it polite here
Okay, well as long as we're keeping it polite, how about we keep it official too? Please read this announcement and our policies. I'm also giving you a warning for breaking the Keep It Organized rule.

If you want help with your problem, please post your latest attempt that proves you've actually tried to solve it yourself. If you continue to ask for code without showing any effort, I'll lock your thread and be forced to give you an infraction.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.