leetcode链表--18、remove-nth-node-from-end-of-list(从链表中删除倒数第k个结点)

题目描述

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.

解题思路:参照找倒数第k个结点那个题

1)两个指针p1 p2  p2先走k-1,然后两个指针一起走,p2走到最后,p1即为倒数第k个结点pre为倒数第k+1个结点

2)从链表中删除第k个节点

本题应注意边界条件的判断:

1)链表为空

2)要删除的正好是第一个结点的情况

 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 == NULL)
13             return NULL;
14         ListNode *pPre = NULL;
15         ListNode *p1 = head;
16         ListNode *p2 = head;
17         for(int i=0;i<n-1;i++)
18         {
19             p2 = p2->next;
20         }
21         while(p2->next != NULL)
22         {
23             pPre = p1;
24             p1 = p1->next;
25             p2 = p2->next;
26         }
27         if (pPre == NULL)//正好要删除的就是第一个结点
28         {
29             head = p1->next;
30             delete p1;
31         }
32         else
33         {
34            pPre->next = p1->next;
35            delete p1;
36         }
37
38         return head;
39     }
40 };
时间: 2024-12-29 04:00:04

leetcode链表--18、remove-nth-node-from-end-of-list(从链表中删除倒数第k个结点)的相关文章

[算法]在单链表和双链表中删除倒数第k个结点

题目: 分别实现两个函数,一个可以删除单链表中倒数第K个节点,另一个可以删除双链表中倒数第K个节点. 要求: 如果链表长度为N,时间复杂度达到O(N),额外空间复杂度达到O(1). 解答: 让链表从头走到尾,每移动一步,就让K值减一,当链表走到结尾时,如果K值大于0,说明不用调整链表,因为链表根本没有倒数第K个节点,此时将原链表直接返回即可:如果K值等于0,说明链表倒数第K个节点就是头结点,此时直接返回head.next,相当于删除了头结点.当K的值小于零时,再次从头结点开始走,每移动一步,就让

Leetcode 线性表 Remove Nth Node From End of List

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Nth Node From End of List Total Accepted: 12160 Total Submissions: 41280 Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1

找出单向链表中的倒数第k个结点

import java.util.Scanner; public class List { private Node first; private int N; class Node{ int data; Node next; } //顺便复习一下链表 public int size() { return N; } public boolean isEmpty() { return first==null; } public Node FindPrev(int pos){ Node tmp=fi

链表中获取倒数第K个结点

/* * 链表中查找倒数第K个结点.cpp * * Created on: 2018年5月1日 * Author: soyo */ #include<iostream> using namespace std; struct Node { int num; Node * next; }; Node * creat() { Node *head=NULL; head=new Node; head->num=9; head->next=NULL; return head; } Node

[Leetcode][Python]19: Remove Nth Node From End of List

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 19: Remove Nth Node From End of Listhttps://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ Given a linked list, remove the nth node from the end of list and return its head. For ex

leetcode_19题——Remove Nth Node From End of List(链表)

Remove Nth Node From End of List Total Accepted: 54129 Total Submissions: 197759My Submissions Question Solution 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,

链表中删除倒数第K个节点

问题描述 分别实现两个函数,一个可以删除单链表中倒数第K个节点,另一个可以删除双链表中倒数第K个节点. 问题分析与解决 从问题当中,我们只能得到一个链表和要删除的第K个节点的信息,于是就有以下思路:如果链表为空或者K<0时,直接返回:如若不然,遍历链表的每个节点,每经过一个节点K减1.比如对于1 --> 2 --> 3 --> 4该链表的过程如下: K = 5,所遍历的节点以及K值的变化:1 -- > 2 --> 3 --> 4 4,3,2,1: K = 4,所遍

链表中的倒数第k个结点

题目描述 输入一个链表,输出该链表中倒数第k个结点. 基本思想:定义两个指针a,b分别指向头节点, a指针先向前走k-1步(注意:因为倒数节点是从倒数第一个结点开始的,而不是零),然后a指针和b指针一起向前移动, 直到a->next == NULL.此时,b指针所指向的结点.即为倒数第K个结点. 边界条件:1.输入的pListHead为空指针的情况:    2.输入的以pListHead为头结点的链表的节点总数少于K的情况: 3.输入K为0的情况. #include <iostream>

LeetCode之“链表”:Remove Nth Node From End of List

题目链接 题目要求: 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. N