首页 > 其他 > 详细

leetcode 19

时间:2016-09-28 15:31:29      阅读:85      评论:0      收藏:0      [点我收藏+]

指针方面的问题

/**
 * 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

原文:http://www.cnblogs.com/thefirstfeeling/p/5916286.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!