标签:|| leetcode col free while nbsp ++ remove code
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。
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 { 11 public: 12 ListNode * removeNthFromEnd(ListNode* head, int n) 13 { 14 ListNode *h = head, *h1; 15 16 if (head != nullptr) 17 { 18 int num = 1; 19 while (h->next != nullptr) 20 { 21 ++num; 22 h = h->next; 23 } 24 if (n == 0 || n > num) 25 return head; 26 else if (n == num) 27 { 28 h = head; 29 head = head->next; 30 free(h); 31 return head; 32 } 33 else 34 { 35 h = head; 36 while (h->next != NULL) 37 { 38 if (num == n + 1) 39 h1 = h; 40 --num; 41 h = h->next; 42 } 43 h = h1->next; 44 h1->next = h->next; 45 free(h); 46 return head; 47 } 48 } 49 else 50 return head; 51 } 52 };
【Leetcode】19. Remove Nth Node From End of List
标签:|| leetcode col free while nbsp ++ remove code
原文地址:https://www.cnblogs.com/sunbines/p/9141095.html