您的位置:首页 > Web前端 > Node.js

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

2014-03-07 10:55 405 查看
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
****************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: