
Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: ->->->->, and n = . After removing the second node from the end, the linked list becomes ->->->.
Note:
Given n will always be valid.
Try to do this in one pass.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
struct ListNode* cur;
struct ListNode* pre;
cur = head;
pre = head;
if(head == NULL)
return head;
while(n)
{
cur = cur->next;
n--;
}
if(cur == NULL){
head = head->next;
return head;
}
while(cur->next != NULL){
cur = cur->next;
pre = pre->next;
}
pre->next = pre->next->next;
return head; }
python版本:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
#这里的头结点是第一个结点
#判断链表是否为空
if head == None:
return head #设置两个标志位
cur = head
pre = head #cur先走n步
while n:
cur = cur.next
n -= 1 #如果cur为空,则说明n为链表长度
if cur == None:
return head.next #pre标志位和cur一起走,直到cur走到最后一个结点
while cur.next != None:
cur = cur.next
pre = pre.next
#删除倒数第n个结点
pre.next = pre.next.next
return head