• leetcode 单链表19


    /**
    <p>给你一个链表,删除链表的倒数第&nbsp;<code>n</code><em>&nbsp;</em>个结点,并且返回链表的头结点。</p>
    
    <p>&nbsp;</p>
    
    <p><strong>示例 1:</strong></p>
    <img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg" style=" 542px; height: 222px;" />
    <pre>
    <strong>输入:</strong>head = [1,2,3,4,5], n = 2
    <strong>输出:</strong>[1,2,3,5]
    </pre>
    
    <p><strong>示例 2:</strong></p>
    
    <pre>
    <strong>输入:</strong>head = [1], n = 1
    <strong>输出:</strong>[]
    </pre>
    
    <p><strong>示例 3:</strong></p>
    
    <pre>
    <strong>输入:</strong>head = [1,2], n = 1
    <strong>输出:</strong>[1]
    </pre>
    
    <p>&nbsp;</p>
    
    <p><strong>提示:</strong></p>
    
    <ul>
    	<li>链表中结点的数目为 <code>sz</code></li>
    	<li><code>1 &lt;= sz &lt;= 30</code></li>
    	<li><code>0 &lt;= Node.val &lt;= 100</code></li>
    	<li><code>1 &lt;= n &lt;= sz</code></li>
    </ul>
    
    <p>&nbsp;</p>
    
    <p><strong>进阶:</strong>你能尝试使用一趟扫描实现吗?</p>
    <div><div>Related Topics</div><div><li>链表</li><li>双指针</li></div></div><br><div><li> 1953</li><li> 0</li></div>
    */
    
    //leetcode submit region begin(Prohibit modification and deletion)
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode() {}
     *     ListNode(int val) { this.val = val; }
     *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {
    		ListNode dummy = new ListNode(-1);
    		dummy.next = head;
    		ListNode p = findFromEnd(dummy,n+1);
    		p.next = p.next.next;
    		return dummy.next;
        }
    
    	/**
    	 * 5个节点,倒数第三个既第2个节点;要删除2就要找到第1个节点
    	 * 5-3=2,传入的虚拟节点既 6-3 = 3;是原先链表的第二节点
    	 * 
    	 * @param head
    	 * @param k
    	 * @return
    	 */
    	ListNode findFromEnd(ListNode head,int k){
    		ListNode p1 = head;
    		for (int i = 0; i <k; i++) {
    			p1 = p1.next;
    		}
    		ListNode p2= head;
    		while(p1!=null){
    			p1=p1.next;
    			p2=p2.next;
    		}
    		return p2;
    	}
    }
    //leetcode submit region end(Prohibit modification and deletion)
    
    
    
  • 相关阅读:
    Finder 的分栏显示模式宽度调整
    IBAction作用相当于void,NSLog(@"被调用的方法名是%s",__func__);
    Trapping Rain Water
    Binary Tree Preorder Traversal
    Valid Parentheses
    Reverse Words in a String | LeetCode OJ | C++
    Gas Station|leetcode 贪心
    两个字符串“相等”
    Binary Tree Zigzag Level Order Traversal
    Add Binary
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/16128706.html
Copyright © 2020-2023  润新知