LeetCode19:Remove Nth Node From End of List
来源:互联网 发布:软件正确性验证 编辑:程序博客网 时间:2024/06/11 17:05
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.
/** * 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) { // Start typing your Java solution below // DO NOT write main() function int dist = 0; ListNode left=head, right=head; while(right.next!=null){ if(dist<n){ right=right.next; dist++; } else{ right=right.next; left=left.next; } } if(left==head && dist<n) head = head.next; else left.next = left.next.next; return head; }}
------------------------------------------------------------------------------------------------------------------------------------------
LL's solution:
/** * 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) { // Start typing your Java solution below // DO NOT write main() function ListNode demi = new ListNode(0); demi.next = head; ListNode current = demi; ListNode end = demi; //move end to the Nth node from head while(end.next!=null && n>0){ end = end.next; n--; } if(n==0){ //move current and end concurrently until end reach the end of list while(end.next!=null){ end = end.next; current = current.next; } //remove current current.next = current.next.next; } return demi.next; }}
Note:
Use a demi node to handle 'removing head' situation.
- LeetCode19:Remove Nth Node From End of List
- [LeetCode19]Remove Nth Node From End of List
- leetcode19-Remove Nth Node From End of List
- LeetCode19——Remove Nth Node From End of List
- Leetcode19 Remove Nth Node From End of List
- LeetCode19:Remove Nth Node From End of List
- leetcode19---Remove Nth Node From End of List
- leetcode19. Remove Nth Node From End of List
- leetcode19.Remove Nth Node From End of List
- leetcode19:Remove Nth Node From End of List
- Leetcode19. Remove Nth Node From End of List
- leetcode19:Remove Nth Node From End of List
- Leetcode19 - Remove Nth Node From End of List(链表)
- leetcode19~Remove Nth Node From End of List
- LeetCOde19. Remove Nth Node From End of List
- LeetCode19. Remove Nth Node From End of List
- LeetCode19. Remove Nth Node From End of List
- LeetCode19:Remove Nth Node From End of List
- Highcharts翻译系列八:Pane窗格选项和PlotOptions绘图选项概览
- oracle游标的定义使用
- xyssl资料整理(一) 对aescrypt2工程的理解
- SQL在线查询设计器2
- 缓存与session的清理
- LeetCode19:Remove Nth Node From End of List
- Spring MVC学习之一:基础入门
- Android小项目二——MySnake
- EJB概述
- PHP cookie和session的分析
- 如何学习智能手机应用开发?
- 如何跳出页面的frameset框架?
- 让Android 变身回一台真正的Linux系统吧!!!
- 省市县的级联案例