[leetcode]19. Remove Nth Node From End of List
来源:互联网 发布:marc元数据知乎 编辑:程序博客网 时间:2024/06/06 07:38
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; } * } */public class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode pre = new ListNode(0); pre.next = head; ListNode ahead = pre,behind = pre; for(int i=0;i<n;i++){ ahead=ahead.next; } while(ahead.next!=null){ ahead=ahead.next; behind=behind.next; } behind.next=behind.next.next; return pre.next; }}
golang代码
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */func removeNthFromEnd(head *ListNode, n int) *ListNode { pre:=&ListNode{ Val:0, Next:head, } ahead:=pre behind:=pre for i:=0;i<n;i++{ ahead=ahead.Next } for ahead.Next!=nil{ ahead=ahead.Next behind=behind.Next } behind.Next=behind.Next.Next return pre.Next }
0 0
- 19. Remove Nth Node From End of List leetcode list
- [LeetCode]19.Remove Nth Node From End of List
- LeetCode 19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List Leetcode Python
- LeetCode --- 19. Remove Nth Node From End of List
- LeetCode 19.Remove Nth Node From End of List
- [Leetcode] 19. Remove Nth Node From End of List
- [leetcode] 19.Remove Nth Node From End of List
- <LeetCode OJ> 19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List LeetCode
- leetcode 19. Remove Nth Node From End of List
- [LeetCode OJ] 19. Remove Nth Node From End of List
- LeetCode 19. Remove Nth Node From End of List
- Leetcode:19. Remove Nth Node From End of List(JAVA)
- leetcode 19. Remove Nth Node From End of List
- Leetcode 19. Remove Nth Node From End of List
- 【LeetCode】19. Remove Nth Node From End of List
- leetcode 19.Remove Nth Node From End of List
- 日常实用tips
- java基础知识点总结
- Android练习-----列表选择框(Spinner) (上)
- Codeforces Round #376 (Div. 2) F. Video Cards (二分)(lower bound)
- 位运算符
- [leetcode]19. Remove Nth Node From End of List
- ubuntu14安装tensorflow
- leetcode submission/20161018(move zeroes)
- 算法练习16
- 一个关于概率的问题的思考
- linux不带缓存的文件操作
- unity shader (6)--实现高光反射光照模型
- 算法Week08.03 - LeetCode 112. Path Sum
- MFC----------创建用户登录界面笔记