Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for ListNode.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int val) {
- * this.val = val;
- * this.next = null;
- * }
- * }
- */
- public class Solution {
- /**
- * @param head: The first node of linked list.
- * @param n: An integer.
- * @return: Nth to last node of a singly linked list.
- */
- ListNode nthToLast(ListNode head, int n) {
- if (head == null || n < 1) {
- return null;
- }
- ListNode p1 = head;
- ListNode p2 = head;
- for (int j = 0; j < n - 1; ++j) {
- if (p2 == null) {
- return null;
- }
- p2 = p2.next;
- }
- while (p2.next != null) {
- p1 = p1.next;
- p2 = p2.next;
- }
- return p1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment