174. 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 will always be valid.
Try to do this in one pass.

 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         if (head == nullptr || head->next == nullptr)
13             return nullptr;
14         
15         ListNode *pre = head;
16         ListNode *cur = nullptr;
17         
18         for (int i = 0; i < n-1; i++)
19             if (pre->next != nullptr)
20             {
21                 pre = pre->next;
22             }
23             else
24             {
25                 return nullptr;
26             }
27             
28         pre = pre->next;
29         if (pre == nullptr)
30         return head->next;
31         
32         cur = head;
33         while(pre->next != nullptr)
34         {
35             pre = pre->next;
36             cur = cur->next;
37         }
38         
39         cur->next = cur->next->next;
40         return head;
41     }
42 };

 

 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         if (head == nullptr || head->next == nullptr)
13             return nullptr;
14         
15         ListNode *pre = head;
16         ListNode *cur = nullptr;
17         
18         for (int i = 0; i < n; i++)
19             if (pre != nullptr)
20             {
21                 pre = pre->next;
22             }
23             else
24             {
25                 return nullptr;
26             }
27             
28         
29         if (pre == nullptr)
30         return head->next;
31         
32         cur = head;
33         while(pre->next != nullptr)
34         {
35             pre = pre->next;
36             cur = cur->next;
37         }
38         
39         cur->next = cur->next->next;
40         return head;
41     }
42 };

 

 1 // 面试题22:链表中倒数第k个结点
 2 // 题目:输入一个链表,输出该链表中倒数第k个结点。为了符合大多数人的习惯,
 3 // 本题从1开始计数,即链表的尾结点是倒数第1个结点。例如一个链表有6个结点,
 4 // 从头结点开始它们的值依次是1、2、3、4、5、6。这个链表的倒数第3个结点是
 5 // 值为4的结点。
 6 
 7 #include <cstdio>
 8 #include "..\Utilities\List.h"
 9 
10 ListNode* FindKthToTail(ListNode* pListHead, unsigned int k)
11 {
12     if(pListHead == nullptr || k == 0)
13         return nullptr;
14 
15     ListNode *pAhead = pListHead;
16     ListNode *pBehind = nullptr;
17 
18     for(unsigned int i = 0; i < k - 1; ++ i)
19     {
20         if(pAhead->m_pNext != nullptr)
21             pAhead = pAhead->m_pNext;
22         else
23         {
24             return nullptr;
25         }
26     }
27 
28     pBehind = pListHead;
29     while(pAhead->m_pNext != nullptr)
30     {
31         pAhead = pAhead->m_pNext;
32         pBehind = pBehind->m_pNext;
33     }
34 
35     return pBehind;
36 }

 

posted @ 2017-10-23 14:02  绿宝宝怪  阅读(146)  评论(0)    收藏  举报
#site_nav_under,#ad_under_post_holder,#under_post_news,#google_ad_c2,#under_post_kb{ width:0; height:0; display:none; overflow:hidden; }