博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Remove Nth Node From End of List
阅读量:4074 次
发布时间:2019-05-25

本文共 1086 字,大约阅读时间需要 3 分钟。

Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head.

For 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.
Try to do this in one pass.

Java代码:

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public ListNode removeNthFromEnd(ListNode head, int n) {        int len =1;        ListNode tmp =head;        ListNode tmp_2;         while(tmp.next != null){        	tmp = tmp.next;        	len++;        }        if(n==len){        	tmp_2 = head.next;        	head.next=null;        	return tmp_2;        }        len=len-n-1;        if(len<0)return null;        tmp =head;        while(len>0){        	tmp=tmp.next;        	len--;        }        tmp_2 = tmp.next;        tmp.next=tmp.next.next;        tmp_2.next=null;        return head;    }}

转载地址:http://mjuni.baihongyu.com/

你可能感兴趣的文章
字符串的截取
查看>>
2. Add Two Numbers
查看>>
17. Letter Combinations of a Phone Number (DFS, String)
查看>>
93. Restore IP Addresses (DFS, String)
查看>>
19. Remove Nth Node From End of List (双指针)
查看>>
49. Group Anagrams (String, Map)
查看>>
139. Word Break (DP)
查看>>
Tensorflow入门资料
查看>>
剑指_用两个栈实现队列
查看>>
剑指_顺时针打印矩阵
查看>>
剑指_栈的压入弹出序列
查看>>
剑指_复杂链表的复制
查看>>
服务器普通用户(非管理员账户)在自己目录下安装TensorFlow
查看>>
星环后台研发实习面经
查看>>
大数相乘不能用自带大数类型
查看>>
字节跳动后端开发一面
查看>>
CentOS Tensorflow 基础环境配置
查看>>
centOS7安装FTP
查看>>
FTP的命令
查看>>
CentOS操作系统下安装yum的方法
查看>>