码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode题解之Remove Nth Node From End of List

时间:2019-02-22 12:15:28      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:from   题目   remove   class   技术   leetcode   ima   com   div   

1、题目描述

技术图片

 

2、问题分析

直接计算,操作。

3、代码

 1 ListNode* removeNthFromEnd(ListNode* head, int n) {
 2         if (head == NULL)
 3             return head;
 4         int len = 0;
 5         ListNode *p = head;
 6         while (p != NULL) {
 7             len++;
 8             p = p->next;
 9         }
10         
11         int step = len - n;
12         if (step == 0)
13             return head->next;
14         p = head;
15         while (--step) {
16             p = p->next;
17         }
18         
19         ListNode *tmp = p->next->next;
20         p->next = tmp;
21         
22         
23         return head;
24         
25         
26     }

 

LeetCode题解之Remove Nth Node From End of List

标签:from   题目   remove   class   技术   leetcode   ima   com   div   

原文地址:https://www.cnblogs.com/wangxiaoyong/p/10417131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!