/** 链表节点 */ classListNode { var val: Int var next: ListNode? init(_val: Int) { self.val = val self.next =nil } }
funcremoveNthFromEnd(_head: ListNode?, _n: Int) -> ListNode? { let newHead =ListNode(0) newHead.next = head var pre: ListNode? = newHead var cur = head var fast = head for_in0..< n { if fast !==nil { fast = fast?.next } else { return head } } while fast !=nil { fast = fast?.next pre = cur cur = cur?.next } pre?.next = cur?.next return newHead.next }