面试题22:链表中倒数第K个节点

19 篇文章 1 订阅

题目:输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第一个节点。

c++实现:

#include<iostream>
#include<stack>
using namespace std;
struct ListNode
{
	int m_nkey;
	ListNode* m_pNext;	
};

 //建立一个链表用头插法
 void AddToHead(ListNode** pHead,int value)
{
 	 
 	ListNode* pNew = new ListNode();
 	pNew->m_nkey = value;
 	pNew->m_pNext = NULL;
 	if(*pHead == NULL)
 	{
 		*pHead = pNew;
	}
	else
	{
//	 	ListNode *pNode = *pHead;
//	 	pNew->m_pNext = pNode;
//	 	pNode->m_pNext = pNew;
//	 	*pHead = pNode;
		pNew->m_pNext = *pHead;
		*pHead = pNew;
		
	}
} //删除列表中的节点
 //删除链表中重复的节点

 //找到倒数第k个节点
 ListNode* Find(ListNode *pHead,unsigned int k)
 {
 	if(pHead==NULL || k==0)
 	return NULL;
 	ListNode* pFirst = pHead;
 	ListNode* pBehind;
 	for(int i=0; i<k-1;i++)
	 {
	 	if(pFirst->m_pNext != NULL)
	 	{
	 		pFirst = pFirst->m_pNext;	
		 }
		 else{
		 	return NULL;	
		 } 
	  } 
	pBehind = pHead;
	while(pFirst->m_pNext != NULL)
	{
		pFirst = pFirst->m_pNext;
		pBehind = pBehind->m_pNext;
	}
	return pBehind;  
  } 
  void print(ListNode * pHead)
  {
  	ListNode* pNode = pHead;
	  while(pNode!=NULL)
	  {
	  	cout<<pNode->m_nkey<<" ";
	  	pNode = pNode->m_pNext;
	   } 
   } 
int main()
{
	int num = 10;
	
	ListNode *p  = new ListNode();
	p = NULL;
	ListNode **pHead = &p;
       AddToHead(pHead,1);
	   AddToHead(pHead,2);	
	   AddToHead(pHead,3);	
	   AddToHead(pHead,4);	
	   AddToHead(pHead,5);	
	   AddToHead(pHead,6);	
	   AddToHead(pHead,7);		   		   	   	
	   //DeleteDuplication(pHead);
	  
	   ListNode *res =  Find(*pHead, 4);
	   cout<<res->m_nkey;
//	ListNode *del = new ListNode();
//	print(*pHead);
 } 
 
 

JAVA实现:

package jianzhi_offer;

public class DeleteDuplication {

	public static class ListNode{
		private int data;
		private ListNode next;
		public ListNode(int data,ListNode next)
		{
			this.data = data;
			this.next = next;
		}
	}
	public static ListNode Find(ListNode head, int k)
	{
		if(head == null || k==0)
		{
			return null;
		}
		ListNode firstNode;
		ListNode behind;
		firstNode = head;
		for(int i=0; i<k-1; i++)
		{
			if(firstNode.next!=null)
			{
				firstNode = firstNode.next;
			}
			else {
				return null;
			}
		}
		behind = head;
		while(firstNode.next!=null)
		{
			firstNode = firstNode.next;
			behind = behind.next;
		}
			return behind;	
	}	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ListNode tail = new ListNode(7,null);
		ListNode c = new ListNode(6,tail);
		ListNode b = new ListNode(5,c);
		ListNode a = new ListNode(4,b);
		ListNode e = new ListNode(3,a);
		ListNode d = new ListNode(2,e);
		ListNode head = new ListNode(1,d);
	  ListNode res = Find(head,2);
	  if(res!=null)
	  System.out.println(res.data+"  ");
		while(head!=null)
		{
			System.out.println(head.data + " ");
			head = head.next;
		}
	}

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值