★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-rdgzueej-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a linked list, remove the n-th node from the end of list and return its head.node
Example:git
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:github
Given n will always be valid.微信
Follow up:app
Could you do this in one pass?post
給定一個鏈表,刪除鏈表的倒數第 n 個節點,而且返回鏈表的頭結點。this
示例:spa
給定一個鏈表: 1->2->3->4->5, 和 n = 2. 當刪除了倒數第二個節點後,鏈表變爲 1->2->3->5.
說明:code
給定的 n 保證是有效的。
進階:
你能嘗試使用一趟掃描實現嗎?
12ms
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { 14 var endPointer : ListNode? = head 15 var nFromEndPointer : ListNode? = nil 16 var counter = n 17 18 while endPointer != nil { 19 if counter == 0 { 20 if nFromEndPointer == nil { 21 nFromEndPointer = head 22 } else { 23 nFromEndPointer = nFromEndPointer?.next 24 } 25 } 26 27 endPointer = endPointer?.next 28 29 if counter > 0 { 30 counter -= 1 31 } 32 } 33 34 if nFromEndPointer == nil { 35 if counter == 0 { 36 return head?.next 37 } 38 return nil 39 } else { 40 nFromEndPointer?.next = nFromEndPointer?.next?.next 41 } 42 43 return head 44 } 45 }
16ms
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { 14 let dummy = ListNode(0) 15 var slow: ListNode? = dummy 16 var fast: ListNode? = dummy 17 18 slow?.next = head 19 for _ in 1...(n + 1) { 20 fast = fast?.next 21 } 22 while fast != nil { 23 slow = slow?.next 24 fast = fast?.next 25 } 26 slow?.next = slow?.next?.next 27 28 return dummy.next 29 } 30 }
20ms
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { 14 guard let head = head else { 15 16 return nil 17 } 18 19 20 if n == 1 && head.next == nil { 21 22 return nil 23 } 24 25 var cur: ListNode? = head 26 var last: ListNode? = head 27 28 for i in 1..<n { 29 30 last = last?.next 31 } 32 33 var prev: ListNode? 34 while last?.next != nil { 35 prev = cur 36 cur = cur?.next 37 last = last?.next 38 } 39 40 prev?.next = prev?.next?.next 41 42 return prev == nil ? head.next : head 43 } 44 }
20ms
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { 14 var result:ListNode? = head; 15 var tempNode:ListNode? = result; 16 var tempNode2:ListNode? = result; 17 if head! == nil { 18 return head; 19 } 20 21 var i = 0; 22 while i < n && tempNode!.next != nil { 23 tempNode = tempNode!.next; 24 i += 1; 25 } 26 while (tempNode!.next != nil) { 27 i += 1; 28 tempNode = tempNode!.next; 29 tempNode2 = tempNode2!.next; 30 } 31 if (i + 1 == n) { 32 return result!.next; 33 }else { 34 tempNode2!.next = tempNode2!.next!.next; 35 return result; 36 } 37 } 38 }
24ms
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { 14 var first: ListNode? = head 15 var n:Int = n 16 while(n-- != 0) 17 { 18 first=first!.next 19 } 20 if first == nil 21 { 22 return head!.next 23 } 24 var sec: ListNode? = head 25 while(first!.next != nil) 26 { 27 sec = sec!.next 28 first = first!.next 29 } 30 sec!.next = sec!.next!.next 31 return head 32 } 33 } 34 35 /*擴展Int類,實現自增++、自減--運算符*/ 36 extension Int{ 37 //後綴--:先執行表達式後再自減 38 static postfix func --(num:inout Int) -> Int { 39 //輸入輸出參數num 40 let temp = num 41 //num減1 42 num -= 1 43 //返回減1前的數值 44 return temp 45 } 46 }
24ms
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { 14 guard let _ = head else { 15 return nil 16 } 17 18 var preNode = head 19 var curNode = head 20 var count: Int = 0 21 while count < n { 22 curNode = curNode?.next 23 count += 1 24 } 25 if curNode == nil { 26 return preNode?.next 27 } 28 29 while let _ = curNode?.next { 30 preNode = preNode?.next 31 curNode = curNode?.next 32 } 33 34 preNode?.next = preNode?.next?.next 35 return head 36 } 37 }
28ms
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { 14 var resultArray = [ListNode]() 15 var tmpNode = head 16 var resultNode = head 17 repeat { 18 resultArray.append(tmpNode!) 19 tmpNode = tmpNode?.next 20 }while tmpNode != nil 21 22 let arrayCount = resultArray.count 23 if arrayCount == n { 24 resultNode = head?.next 25 return resultNode 26 } 27 resultArray[arrayCount - n - 1].next = resultArray[arrayCount - n].next 28 return resultNode 29 } 30 }