Remove Nth Node From End of List

来源:互联网 发布:0.14.1死亡不掉落js 编辑:程序博客网 时间:2024/06/06 05:34
public class Solution {    public ListNode removeNthFromEnd(ListNode head, int n) {         if (head == null || n==0) return null;        ListNode listNode = new ListNode(0);        listNode.next = head;        ListNode p = listNode;        ListNode q = listNode;        for (int i=0; i<n; i++){            if (p.next != null) p = p.next;            else return head;        }        while (p.next!=null){            p = p.next;            q = q.next;        }        ListNode tmp = q;        q.next = q.next.next;        return  listNode.next;    }}

0 0
原创粉丝点击