【LeetCode】Remove Nth Node From End of List && 【九度】题目1517:链表中倒数第k个结点

133 篇文章 0 订阅
121 篇文章 2 订阅
1、Remove Nth Node From End of List
      Total Accepted: 8400 Total Submissions: 28316
      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.
      声明一个新的ListNode指向head,到倒数第n+1个点的时候,将nextset为next.next。注意n和链表长度相等以及n为0的情况。

Java AC

/**
 * 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) {
        if (n == 0) {
			return head;
		}
		ListNode point = head;
		int allLen = 0;
		while (point != null) {
			point = point.next;
			allLen++;
		}
		if (allLen == n) {
			return head.next;
		}
		int num = 0;
		point = head;
		while (point != null && num != allLen - n - 1) {
			point = point.next;
			num++;
		}
		if (point.next != null) {
			ListNode tempNode = point.next.next;
			point.next = tempNode;
		}
		return head;
    }
}
2、题目1517:链表中倒数第k个结点

时间限制:1 秒内存限制:128 兆特殊判题:否提交:557解决:286
题目描述:
输入一个链表,输出该链表中倒数第k个结点。
(hint: 请务必使用链表。)
输入:
输入可能包含多个测试样例,输入以EOF结束。
对于每个测试案例,输入的第一行为两个整数n和k(0<=n<=1000, 0<=k<=1000):n代表将要输入的链表元素的个数,k代表要查询倒数第几个的元素。
输入的第二行包括n个数t(1<=t<=1000000):代表链表中的元素。
输出:
对应每个测试案例,
若有结果,输出相应的查找结果。否则,输出NULL。
样例输入:
5 2
1 2 3 4 5
1 0
5
样例输出:
4
NULL
这个题和remove那个差不多,不过这个要简单点。但是得建立链表。其实如果不要求的话,数组完全可以实现么。

Java AC

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
 
public class Main {
    /*
     * 1517
     */
    public static void main(String[] args) throws Exception {
        StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            int n = (int) st.nval;
            st.nextToken();
            int k = (int) st.nval;
            LinkedNode node = null;
            LinkedNode point = null;
            for (int i = 0; i < n; i++) {
                st.nextToken();
                LinkedNode tempNode = new LinkedNode((int) st.nval ,null);
                if (point == null) {
                    node = tempNode;
                    point = tempNode;
                }else {
                    point.setNext(tempNode);
                    point = point.getNext();
                }
            }
            int i = 0;
            while (node != null && i != n - k) {
                node = node.next;
                i++;
            }
            if (node == null) {
                System.out.println("NULL");
            }else {
                System.out.println(node.getData());
            }
        }
    }
    static class LinkedNode{
        private int data;
        private LinkedNode next;
        public int getData() {
            return data;
        }
        public void setData(int data) {
            this.data = data;
        }
        public LinkedNode getNext() {
            return next;
        }
        public void setNext(LinkedNode next) {
            this.next = next;
        }
        public LinkedNode(int data, LinkedNode next) {
            super();
            this.data = data;
            this.next = next;
        }
        public LinkedNode() {
            super();
        }
    }
     
}
/**************************************************************
    Problem: 1517
    User: wzqwsrf
    Language: Java
    Result: Accepted
    Time:1120 ms
    Memory:28588 kb
****************************************************************/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值