To find the kth to Last Element of a Singly Linked List

To find the kth to Last Element of a Singly Linked List

  • To find the kth to Last Element of a Singly Linked List

    • Web Link
    • Description
    • Code - C
    • Tips

Web Link

None

Description

Write a pro-gram to find the kth to Last Ele-ment of a Singly Linked List

For example:

Orig-i-nal List : ->1->2->8->3->7->0->4

Out-put : 3rd Ele-ment from the end is : 7

Code - C++

  • 片段
ListNode* findkthtolast(ListNode *head, int k) {
    ListNode* runner = head;
    ListNode* chaser = head;
    if (head == NULL || k < 0) {
        return NULL;
    }
    for (int i = 0; i < k; i++) {
        runner = runner->next;
    }
    while (runner != NULL) {
        chaser = chaser->next;
        runner = runner->next;
    }
    return chaser;
}
  • 完整(包括测试)
#include<iostream>
using namespace std;
class ListNode{
public:
    int val;
    ListNode* next;
    ListNode(const int val, ListNode* nextNode = NULL) :val(val), next(nextNode){

    }
};
ListNode* findkthtolast(ListNode *head, int k) {
    ListNode* runner = head;
    ListNode* chaser = head;
    if (head == NULL || k < 0) {
        return NULL;
    }
    for (int i = 0; i < k; i++) {
        runner = runner->next;
    }
    while (runner != NULL) {
        chaser = chaser->next;
        runner = runner->next;
    }
    return chaser;
}
void findkthtolasttest() {
    ListNode* head = new ListNode(1, NULL);
    ListNode* node = head;
    node->next = new ListNode(2, NULL);
    node = node->next;
    node->next = new ListNode(8, NULL);
    node = node->next;
    node->next = new ListNode(4, NULL);
    node = node->next;
    node->next = new ListNode(7, NULL);
    node = node->next;
    node->next = new ListNode(0, NULL);
    node = node->next;
    node->next = new ListNode(4, NULL);
    node = node->next;
    node->next = NULL;
    cout << findkthtolast(head,3)->val << endl;
}
int main() {
    findkthtolasttest();
    return 0;
}

Tips

None

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-29 23:35:48

To find the kth to Last Element of a Singly Linked List的相关文章

[CareerCup] 2.2 Kth to Last Element of Linked List 链表的倒数第k个元素

2.2 Implement an algorithm to find the kth to last element of a singly linked list. 这道题让我们求链表中倒数第k个元素,LeetCode中相类似的题目有Kth Largest Element in an Array 数组中第k大的数字 和 Kth Smallest Element in a BST 二叉搜索树中的第K小的元素.但那两道题和这题又不一样,首先这道题是要在链表中操作,链表的特点就是不能通过下标来直接访

2.2 Kth element linked list

Problem Implement an algorithm to find the kth to last element of a singly linked list. Solution 1 public static ListNode findElement(ListNode head, int k) { 2 if(k <= 0) return null; 3 4 ListNode fast = head; 5 ListNode slow = head; 6 7 for(int i=0;

Cracking the Coding Interview Q2.2

Implement an algorithm to find the kth to last element of a singly linked list. 思路:双指针 参考 http://www.cnblogs.com/jdflyfly/p/3810697.html Cracking the Coding Interview Q2.2,布布扣,bubuko.com

CTCI 2.2

Implement an algorithm to find the kth to last element of a singly linked list. Classical "Runner" Technique of linkedlist /*Use two pointers, forward one K nodes, then the two pointers form a "window" contains K nodes. Then move two p

Linux内核之vmlinux与vmlinuz

[题目] 原文: 2.2 Implement an algorithm to find the nth to last element of a singly linked list. 译文: 实现一个算法从一个单链表中返回倒数第n个元素. [分析] (1)创建两个指针p1和p2,指向单链表的开始节点. (2)使p2移动n-1个位置,使之指向从头开始的第n个节点.(意思是就是使p1和p2距离n个位置) (3)接下来检查p2 - > = = null 如果yes返回p1的值,否则继续移动p1和 p

Lintcode: Nth to Last Node in List

Find the nth to last element of a singly linked list. The minimum number of nodes in list is n. Example Given a List 3->2->1->5->null and n = 2, return node whose value is 1. Runner Technique: 两个指针都从Dummy node出发,结束条件是runner.next!=null 1 public

Cracking the coding interview汇总目录

很久之前刷的CTCI的题目,都快忘记了,做个分类汇总,再重新好好复习一遍. Chapter 1 | Arrays and Strings 1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? 1.2 Write code to reverse a C-Style String. (C-Str

Nth to Last Node in List

Find the nth to last element of a singly linked list. The minimum number of nodes in list is n. Example Given a List  3->2->1->5->null and n = 2, return node  whose value is 1. 分析: 要找到nth to last element,我们需要两个指针,第一个指针先走n步,然后两个指针同时走,知道第一个指针为nu

lintcode-easy-Nth to Last Node in List Show result

Find the nth to last element of a singly linked list. The minimum number of nodes in list is n. Given a List  3->2->1->5->null and n = 2, return node  whose value is 1. /** * Definition for ListNode. * public class ListNode { * int val; * List