LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11 ListNode *removeNthFromEnd(ListNode *head, int n) {
12     struct ListNode *q=0,*w=0,*e=0;
13     int i,j=0;
14     if(n==0)    return head;    //无需修改
15     if(n==1&&head->next==0)        return 0;    //只有一个结点,并且要删除掉它
16     q=e=head;                //e为最后一个结点的位置
17     for(i=0;i<n;i++){            //e先设置在q的后面为n个距离的地方,q->next是要删的结点
18         e=e->next;
19         j++;
20         if(e->next==0)    //针对刚好要求删掉链表的头结点
21             break;
22     }
23     while(e->next!=0){        //将e和q保持相同距离,往后移直到e指向最后一个元素,q->next就是所要删除的结点了
24         e=e->next;
25         q=q->next;
26     }
27     if(j<n)    // 对于需要删除头结点的情况,用j判断
28         return head->next;
29     else
30         q->next=q->next->next;
31     return head;
32 }
33 };

题意:给一个链表,要删除从链尾数起,第n个结点,然后返回此链。

思路:分几种情况,一是要删除尾结点,即n=1;二是要删除头结点,需要特殊判断,同时又要缩小代码量(若加多几行代码就容易写多了)。

测试一直出问题,没考虑好只有两个结点的情况。当有两个结点的链表时,n=1或n=2的情况处理好了,后面一般没问题了。

时间: 2024-10-29 19:06:52

LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点的相关文章

lintcode 容易题:Remove Nth Node From End of Lis 删除链表中倒数第n个节点

题目: 删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点.  样例 给出链表1->2->3->4->5->null和 n = 2. 删除倒数第二个节点之后,这个链表将变成1->2->3->5->null. 注意 链表中的节点个数大于等于n 解题: 要删除倒数第n个节点,我们要找到其前面一个节点,也就是倒数第n+1的节点,找到这个节点就可以进行删除.和上题的思想很类似, 定义两个指针,p和cur,cur指针向前走,走了n

[LeetCode]78. Remove Nth Node From end of List删除链表中倒数第N个节点

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:Given n

[LeetCode]19. Remove Nth Node From End of List删除链表的倒数第N个节点

Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n w

19. Remove Nth Node From End of List(删除链表中的第n个结点)

Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n w

[LeetCode] Remove Nth Node From End of List 溢出链表倒数第N个节点

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given

LeetCode: Remove Nth Node From End of List [019]

[题目] Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: G

[leetcode]Remove Nth Node From End of List @ Python

原题地址:http://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ 题意: Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second n

LeetCode: Remove Nth Node From End of List 解题报告

Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Question Solution Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5,

leetCode 19.Remove Nth Node From End of List(删除倒数第n个节点) 解题思路和方法

Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes