zoukankan      html  css  js  c++  java
  • [Swift]LeetCode19. 删除链表的倒数第N个节点 | Remove Nth Node From End of List

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/9890204.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    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?

    给定一个链表,删除链表的倒数第 个节点,并且返回链表的头结点。

    示例:

    给定一个链表: 1->2->3->4->5, 和 n = 2.
    
    当删除了倒数第二个节点后,链表变为 1->2->3->5.
    

    说明:

    给定的 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 }
  • 相关阅读:
    jq获取滚动条高度
    用jq实现简单的tab选项卡
    bootstrap基础css样式1
    c3中的弹性盒子
    第一天注册
    Windows平台下结合 tortoiseSVN 和 VisualSVN Server 搭建SVN服务器并实现 web 站点同步
    【转载】IIS出现“HTTP 错误 500.0,C:phpphp-cgi.exe
    【转载】PHP5.3 配置文件php.ini-development和php.ini-production的区别
    解决 Composer-Setup.exe 安装过程中的报错
    IIS配置页面重写(配合插件url-rewrite2去除页面后缀名)
  • 原文地址:https://www.cnblogs.com/strengthen/p/9890204.html
Copyright © 2011-2022 走看看