Leetcode c语言-Remove Nth Node From End of List
来源:互联网 发布:宝石蓝颜色数据 编辑:程序博客网 时间:2024/06/06 01:53
Title:
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.
这道题目如果用一般的方法,非常容易,首先就是算出链表的长度,然后根据长度和n的关系进行处理,只需要当遇到要remove的节点事,将它的前一个节点 head->next=head->next->next,也就是跳过这个节点即可:
solution:
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { int i=0; int len=0; struct ListNode* temp_head =head; struct ListNode* result_head =head; while(temp_head != NULL) { len++; temp_head=temp_head->next; } if (len==1) return NULL; while(i<len-n-1){ result_head=result_head->next; i++; } if ((len-n) == 0) head=head->next; else result_head->next=result_head->next->next; return head; }
但是这种方法有两个遍历,题目要求说最好使用一个pass,那么就要想,肯定不能全部遍历完,再替换,因为这种操作是不存在的。因此要在遍历过程中,进行替换。那么如何确定要替换的节点的位置呢,这里就需要用到两个指针,第一个指针标明位置,比如第一个指针先向后移动n个,这个时候剩余的长度为len-n,这个长度正好是我们需要的,然后再用第二个指针,该指针和第一个指针同时向后移动,当第一个指针移到最后,那么第二个指针也就移到了len-n的位置,然后删掉该节点即可。
在这种操作中,要注意一点,当第一个指针移动n个后,该指针是NULL,那么就说明,n为长度len了。这种情况只需要将第一个节点删掉即可:head=head->next.
贴出代码:
solution:
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { int i=0; int len=0; struct ListNode* temp_head =head; struct ListNode* result_head =head; if (!head->next) return NULL; for (i=0;i<n;i++) { temp_head=temp_head->next; } if (!temp_head) return head->next; while(temp_head->next) { temp_head = temp_head->next; result_head = result_head->next; } result_head->next = result_head->next->next; return head; }
阅读全文
0 0
- Leetcode c语言-Remove Nth Node From End of List
- [C++]LeetCode: 4 Remove Nth Node From End of List
- [leetcode-]Remove Nth Node From End of List(C)
- 【LeetCode-19】Remove Nth Node From End of List(C++)
- 【C++】【LeetCode】19. Remove Nth Node From End of List
- [C语言][LeetCode][19]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(C语言实现)
- LeetCode[Linked List]: Remove Nth Node From End of List
- 19. Remove Nth Node From End of List leetcode list
- [Leetcode]LeetCode 19:Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- [Leetcode] Remove Nth Node From End of List
- LeetCode: Remove Nth Node From End of List
- leetcode 40: Remove Nth Node From End of List
- [LeetCode] Remove Nth Node From End of List
- Leetcode 19 Remove Nth Node From End of List
- 用天平找小球
- 集合 接口与接口的区别
- 数据库事务隔离级别-- 脏读、幻读、不可重复读(清晰解释)
- 【网安随笔】隐写 Burpsuit伪IP & Trid分析工具 & Wireshark抓包
- 如何使用Git将本地代码上传到github上【图文】
- Leetcode c语言-Remove Nth Node From End of List
- Mac OSX开启原生的ntfs磁盘格式支持
- 深度学习语言模型的通俗讲解(Deep Learning for Language Modeling)
- Python调用shell命令的几种方法(在新进程中执行shell命令)
- PAT [A1034]-Head of a Gang
- 2017 ACM/ICPC Asia Regional Qingdao Online 1003 The Dominator of Strings
- java初学者看什么书比较好
- 计算工资
- 关于使用putParcelableArrayList遇到的问题