19. Remove Nth Node From End of List

来源:互联网 发布:浪潮软件官网 编辑:程序博客网 时间:2024/06/05 04:19
public static ListNode removeNthFromEnd(ListNode head, int n) {        if(head == null) {            return null;        }        ListNode predel = null,del = null, cur =head;        int count = 0;        while(cur!=null) {            count++;            if(del !=null) {                predel = del;                del = del.next;            }            if(count == n) {                del = head;            }            if(count == n+1) {                predel = head;            }            cur = cur.next;        }        if(predel == null && del !=null) {            head = head.next;        }else if(predel !=null && del !=null) {            predel.next = del.next;        }        return head;    }
0 0
原创粉丝点击