【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.

Note:

Given n will always be valid.

Try to do this in one pass.

就是先让head走n步,然后再找一个head,让两个head一直走直到走到第一个head走到头,此时第二个head应该刚好走到倒数第n+1个,然后正常删除就好了~唯一的问题就是如果删除的是head,那么单独讨论

package testAndfun;

class ListNode{
	int val;
	ListNode next;
	ListNode(int x){
		val = x;
		next = null;
	}
}

public class removeNthFromTheEnd {
	public static void main(String[] args){
		removeNthFromTheEnd rne = new removeNthFromTheEnd();
		ListNode x1 = new ListNode(2);
        ListNode x2 = new ListNode(4);
        ListNode x3 = new ListNode(3);
        ListNode x4 = new ListNode(7);
        ListNode x5 = new ListNode(19);
        x1.next = x2;
        x2.next = x3;
        x3.next = x4;
        x4.next = x5;
        prinf(x1);
        System.out.println();
		prinf(rne.removeNthFromEnd(x1, 1));
	}

	public static void prinf(ListNode head){
		while(head!=null){
			System.out.print(head.val+"->");
			head=  head.next;
		}
	}

	public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode p = head;
        ListNode lenTmp = head;
        int len = 0;
        while(lenTmp!=null){
        	len++;
        	lenTmp = lenTmp.next;
        }

        if(len==n){
        	head = head.next;
        	return head;
        }

        for(int i=0;i<n;i++){
        	p = p.next;
        }
        ListNode pp = head;
        while(p.next!=null){
        	p=p.next;
        	pp = pp.next;
        }
        ListNode tmp = pp.next.next;
        pp.next = tmp;
        return head;
    }
}
时间: 2024-08-04 18:46:05

【Leetcode】Remove Nth Node From End of List的相关文章

【LeetCode】Remove Nth Node From End of List (2 solutions)

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

【leetcode】Remove Nth Node From End of List(easy)

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. 思路: 最基本的思路肯定

【Leetcode】【Easy】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. Note:Given n

【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. Note:Given n

LeetCode:Remove Nth Node From End of List

1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 3.题目内容 英文:Given a linked list, remove the nth node from the end of list and return its head. 中文:给出一个链表,删去链表中的倒数第n项,返回链表首节点 例如:

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

LeetCode OJ - 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.Note:Give

[LeetCode][JavaScript]Remove Nth Node From End of List

https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 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

leetCode 19. Remove Nth Node From End of List 链表

19. 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 lis