LeetCode-19-Remove Nth Node From End of List(C语言实现)
2017-12-24 00:50
316 查看
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
int i;
struct ListNode *p, *q, *tmp;
p = head;
q = head;
for(i = 0; i < n; ++i)
q = q -> next;
if(q == NULL)
{
tmp = p;
p = p -> next;
free(tmp);
return p;
}
while(q -> next != NULL)
{
p = p-> next;
q = q -> next;
}
tmp = p -> next;
p -> next = tmp -> next;
free(tmp);
return head;
}
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
int i;
struct ListNode *p, *q, *tmp;
p = head;
q = head;
for(i = 0; i < n; ++i)
q = q -> next;
if(q == NULL)
{
tmp = p;
p = p -> next;
free(tmp);
return p;
}
while(q -> next != NULL)
{
p = p-> next;
q = q -> next;
}
tmp = p -> next;
p -> next = tmp -> next;
free(tmp);
return head;
}
相关文章推荐
- 【LeetCode】19 Remove Nth Node From End of List (c++实现)
- [C语言][LeetCode][19]Remove Nth Node From End of List
- python实现 LeetCode19——Remove Nth Node From End of List
- 【LeetCode】19 Remove Nth Node From End of List (c++实现)
- [leetcode] 19. Remove Nth Node From End of List python实现【easy】
- leetcode_19题——Remove Nth Node From End of List(链表)
- leetcode-19. Remove Nth Node From End of List(删除链表倒数第N个节点)
- LeetCode | 19. Remove Nth Node From End of List
- [leetcode 19] Remove Nth Node From End of List
- LeetCode 19.Remove Nth Node From End of List
- LeetCode (19)Remove Nth Node From End of List
- LeetCode 19 Remove Nth Node From End of List (移除距离尾节点为n的节点)
- LeetCode之19---Remove Nth Node From End of List
- LeetCode 19. Remove Nth Node From End of List
- LeetCode41/19 First Missing Positive/Remove Nth Node From End of List ****
- LeetCode 19. Remove Nth Node From End of List
- [LeetCode]19. Remove Nth Node From End of List
- LeetCode(19)--Remove Nth Node From End of List
- LeetCode 19. Remove Nth Node From End of List
- [LeetCode-Algorithms-19] "Remove Nth Node From End of List" (2017.11.16-WEEK11)