leetcode19Remove Nth Node From End of List

来源:互联网 发布:修改软件下载 编辑:程序博客网 时间:2024/05/23 16:24

leetcode19Remove Nth Node From End of List
思路:

class Solution {public:    ListNode* removeNthFromEnd(ListNode* head, int n) {        int length = 0;        ListNode *h1 = head;        ListNode *tempHead = new ListNode(0);        while(h1!=NULL) {length++;h1 = h1->next;}        int nthFromStart = length - n + 1;        tempHead->next = head;        head = tempHead;        h1 = head;        while(nthFromStart-->1)  h1 = h1->next;        h1->next = h1->next->next;        head = head->next;        return head;       }};