19-remove-nth-node-from-end-of-list
阿新 • • 發佈:2018-11-06
題目描述:
Given a linked list, remove the n-th node from the end of list and return its head.
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.
Follow up:
Could you do this in one pass?
解決程式碼:
package com.jack.algorithm; /** * create by jack 2018/11/1 * * @author jack * @date: 2018/11/1 21:43 * @Description: */ public class RemoveNthNodeFromEndOfList { public static class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public static ListNode removeNthFromEnd(ListNode head, int n) { if (head == null || n ==0) { return head; } head = reverse(head); ListNode p1 = head; ListNode p2 = p1.next; int i = 1; if (n == 1) { head = head.next; } while (p1 != null && i<n) { i++; if (i == n) { p1.next = p2.next; break; } p1 = p1.next; p2 = p2.next; } head = reverse(head); return head; } /** * 建立連結串列 * @param nums * @return */ public static ListNode createListNode(int[] nums){ ListNode head=null; ListNode p = head; for (int i=0;i<nums.length;i++) { int num = nums[i]; ListNode node = new ListNode(num); if (head == null) { head = node; p = head; } else { p.next = node; p = node; } } return head; } /** * 列印連結串列 * @param nodes */ public static void displayListNode(ListNode nodes){ ListNode p = nodes; while (p != null) { int num = p.val; System.out.print(num+"->"); p=p.next; } System.out.println(""); } /** * 連結串列反轉 * @param nodes * @return */ public static ListNode reverse(ListNode nodes){ ListNode head = null; ListNode q = nodes; ListNode t = null; while (q != null) { if (head == null) { head = q; q = q.next; head.next = null; } else { t = q; q=q.next; t.next= head; head = t; } } return head; } public static void main(String[] args) { int[] nums = new int []{1,2,3,4,5}; ListNode listNode = createListNode(nums); displayListNode(listNode); //ListNode node1 = reverse(listNode); ListNode head = removeNthFromEnd(listNode,2); displayListNode(head); } }
原始碼地址: