首页 > 代码库 > leetcode 19

leetcode 19

指针方面的问题

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* removeNthFromEnd(ListNode* head, int n) {            ListNode *The_nth_node,*next,*header_first;            int count=1;           // The_nth_node=(struct ListNode *)malloc(sizeof(ListNode));           // The_nth_node=(struct ListNode *)malloc(sizeof(ListNode));            header_first=head;            The_nth_node=head;            //next=(struct ListNode *)malloc(sizeof(ListNode));            while(head->next!=NULL&&count<=n) {                count++;                head=head->next;            }            if(count==n) {                return The_nth_node->next;            }            while(head->next!=NULL) {                head=head->next;                The_nth_node=The_nth_node->next;            }            next=The_nth_node->next;            The_nth_node->next=next->next;          // free(next);            return header_first;    }};

 

leetcode 19