给定一个链表,删除链表的倒数第 n 个节点并返回链表的头指针
例如,
例如,
给出的链表为:
,
.
删除了链表的倒数第
个节点之后,链表变为
.
删除了链表的倒数第
数据范围: 链表长度
,链表中任意节点的值满足
要求:空间复杂度
,时间复杂度 )
备注:
备注:
题目保证
一定是有效的
struct ListNode* removeNthFromEnd(struct ListNode* head, int n ) {
struct ListNode *phead = (struct ListNode*)malloc(sizeof(struct ListNode)); //设置虚拟头结点
phead->next = head;
//快慢指针
struct ListNode *fast = phead;
struct ListNode *slow = phead;
for (int i=0; i<n; ++i)
{
fast = fast->next;
}
while (fast->next!=NULL)
{
slow = slow->next;
fast = fast->next;
}
slow->next = slow->next->next;
return phead->next;
} #include <stdlib.h>
struct ListNode* removeNthFromEnd(struct ListNode* head, int n ) {
// write code here
struct ListNode* new_head = (struct ListNode*)malloc(sizeof(struct ListNode));
new_head->next = head;
struct ListNode* p = new_head;
struct ListNode* q = new_head;
int sum = 0;
while (p != NULL)
{
sum++;
p = p->next;
}
sum = sum - n - 1;
while (sum)
{
sum--;
q = q->next;
}
q->next = q->next->next;
return new_head->next;
} struct ListNode* removeNthFromEnd(struct ListNode* head, int n ) {
// write code here
int i = 0;
struct ListNode *pre = head; //当前指针的前一个
struct ListNode *cur = head; //当前指针
struct ListNode *nex = head; //与cur相距n
//删除第一个结点,直接返回head->next;
while (pre) {
i++;
pre = pre->next;
}
if (i == n) {
head = head->next;
return head;
}
//nex与cur指针相距n
for (i = 0; i < n; i++){
nex = nex->next;
}
//当nex指向NULL时,cur指向倒数第n个节点
while (nex){
pre = cur;
nex = nex->next;
cur = cur->next;
}
//将倒数第n个节点删除
pre->next = cur->next;
return head;
} struct ListNode* removeNthFromEnd(struct ListNode* head, int n )
{
if(!head)
{
return NULL;
}
struct ListNode *p = head;
struct ListNode *q = head;
struct ListNode *prev;
while(n--)
{
q = q->next;
if(!q && n)
{
p = NULL;
break;
}
}
while(q)
{
prev = p;
p = p->next;
q = q->next;
}
if(p == head)
{
prev = p->next;
p->next = NULL;
return prev;
}
prev->next = p->next;
return head;
// write code here
} struct ListNode* removeNthFromEnd(struct ListNode* head, int n )
{
if (head == NULL)
{
return head;
}
if (head->next == NULL) //只有一个节点
{
free(head);
head = NULL;
return head;
}
int count = 0;
struct ListNode* cur = head;
while (cur)
{
count++;
cur = cur->next;
}
int k = count - n + 1; //正着数找到第n个节点
//头删
cur = head;
if (k == 1)
{
head = head->next;
free(cur);
return head;
}
//中间删除或尾删
struct ListNode* prev = NULL;
for (int i = 1; i < k; i++)
{
prev = cur;
cur = cur->next;
}
prev->next = cur->next;
free(cur);
return head;
}
容易想到的笨方法
struct ListNode* removeNthFromEnd(struct ListNode* head, int n ) {
struct ListNode*res=(struct ListNode*)malloc(sizeof(struct ListNode));
res->next=head;//用res存头结点的位置,以便解决删除头结点的特殊情况
struct ListNode*fast=head;//快慢指针确定所要删除结点的位置
struct ListNode*slow=head;
struct ListNode*pre= res;//pre用来存所要删除结点的前一个结点
int i=0;
for(i=0;i<n;i++)
{
fast=fast->next;
}
while(fast)
{
if(fast->next==NULL)
pre=slow;//pre用来存所要删除结点的前一个结点
fast=fast->next;
slow=slow->next;
}
if(slow->next!=NULL)
{
pre->next=slow->next;
return res->next;
}
pre->next=NULL;
return res->next;
} struct ListNode* removeNthFromEnd(struct ListNode* head, int n ) {
if(head == NULL) return head;
struct ListNode *p = head,*pn = head,*pf;
pf = p;
for(int i = 0;i<n;i++){
if(pn) pn = pn->next;
}
while(pn){
pn = pn->next;
pf = p;
p = p->next;
}
if(pf == p) head = p->next;
else pf->next = p->next;
return head;
// write code here
} struct ListNode* removeNthFromEnd(struct ListNode* head, int n ) {
// write code here
struct ListNode *p=head;
struct ListNode *tmp=head;
struct ListNode *save=NULL;
int count=0;
if(head==NULL)
return head;
while(p!=NULL){
p=p->next;
count++;
}
count=count-n;
if(count<0){
return head;
}else if(count==0){
head=head->next;
return head;
}
while(count){
save=tmp;
tmp=tmp->next;
count--;
}
save->next=tmp->next;
free(tmp);
return head;
} typedef struct ListNode Node;
struct ListNode* removeNthFromEnd(struct ListNode* head, int n ) {
// write code here
//求链表的长度
if(head == NULL)
{
return head;
}
Node *pre = head;
Node *cur =pre->next;
//计算链表的长度
int len = 0;
while(pre)
{
pre = pre->next;
len++;
}
pre = head;
//遍历倒数第n个前面的链表
int m = len-n;
if(m<0)
{
return head;
}else if(m==0)
{
head = head->next;
return head;
}
//找到倒数第n个结点前面的结点
m = m-1;
while(m--)
{
pre = pre->next;
cur = cur->next;
}
Node* next = cur->next;
//让倒数第n个前面的结点指向倒数第n个后面的那个结点,并删除到时第n个结点
pre->next = next;
free(cur);
return head;
}
//快慢指针
快指针先走n步快慢相差N步
struct ListNode* removeNthFromEnd(struct ListNode* head, int n ) {
// write code here
if(!head)
return NULL;
struct ListNode* a=(struct ListNode*)malloc(sizeof(struct ListNode));
a->next=head;
struct ListNode* fast=a;
struct ListNode* slow=a;
for(int i=0;i<n;i++){
fast=fast->next;
}
struct ListNode* cur=slow;
while(fast){
fast=fast->next;
cur=slow;
slow=slow->next;
}
cur->next=cur->next->next;
return a->next;
}