Leetcode - Valid Palindrome
Leetcode - Palindrome Linked List

Leetcode - Remove Nth Node From End of List

violet posted @ Jun 25, 2020 05:20:11 AM in 算法 with tags Algorithm Golang Linked List , 347 阅读

https://leetcode.com/problems/remove-nth-node-from-end-of-list/

Given a linked list, remove the n-th node from the end of list and return its head.

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.

Follow up:

Could you do this in one pass?

 

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func removeNthFromEnd(head *ListNode, n int) *ListNode {
    fast := head
    for i := 0 ; i < n; i++ {
        fast = fast.Next
    }
    if fast == nil {
        // n == list length
        return head.Next
    }
    slow := head
    for fast.Next != nil {
        fast = fast.Next
        slow = slow.Next
    }
    
    slow.Next = slow.Next.Next
    return head
}
1st Inter Blueprint 说:
Feb 12, 2023 03:07:29 PM

Students now acquire the +1, Blueprint 2024 Understudies Must Get the Plus One Blueprint in Pdf Format. After checking the Plus-1, Blueprint 2024 11th Subjects Blueprint English/Tamil Medium, choose the Download option. 1st Inter Blueprint 2024 Take a print of the 11th New Blueprints 2024 for future use in attending the 11th Final Exam. The +1 Blueprint 2024 is the Permission Letter for Every Student.

SCERT Uttarakhand 2 说:
Jul 16, 2023 09:14:41 PM

The SCERT Uttarakhand 2nd Class Syllabus 2024 All Subject Chapter Wise Students Prepare for the Upcoming 2nd Exam, it is quite Essential that they have the Complete Knowledge of the Uttarakhand 2nd Class Latest Syllabus 2024 of All Relevant Subjects, Knowing the Details of Prescribed Topics and the weight age Allotted to Them Makes it easy to plan your Studies Meticulously so that SCERT Uttarakhand 2nd Class Syllabus 2024 you can make Effective Preparations for your Exam and obtain desired marks.All the Students who are Going to Appear for the Examination can make use of the Information Provided here.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter