Contest 7B Remove Nth Node From End of List

Problem B: Remove Nth Node From End of List

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 221   Solved: 68
[ Submit][ Status][ Web Board]

Description

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.

Linked list is recommended here for you to solve this problem.

A sample of the definition of singly linked list:

// Singly-linked list node

  

typedef int element_type;

typedef struct STRUCTNODE* node_ptr;

  

typedef struct STRUCTNODE

{

  element_type element;

  struct STRUCTNODE* next;

}node;

  

  

//typedef struct node* node_ptr;

//typedef node* Link;

/** \brief Linked list implementation

 *

 *

 */

typedef  struct LINKLIST{

node_ptr  head; // Point to list header

node_ptr tail; // Pointer to last

node_ptr fence;// Last element on left

int leftcnt;      // Size of left

int rightcnt;     // Size of right

}*LList ;


Input

You will have several groups of input. The first line indicates the number of inputs.

Each line of inputs contains a string of number, and an index of number need to remove, seperated by a space.

Output

Please output the sting after removing the required number.

Sample Input

2
20171128 4
515021910497 5

Sample Output

2017128
51502190497

HINT

#include <stdio.h>
#include <stdlib.h>

struct Node;
typedef struct Node * List;
typedef struct Node * Position;
struct Node {
	int Element;
	Position Next;
	//when we use in both direction
	//Position Previous;
};
Position ApplyNode(int data)
{
	Position newnodeptr = NULL;
	newnodeptr = (Position)malloc(sizeof(struct Node));

	newnodeptr->Element = data;
	newnodeptr->Next = NULL;
	return newnodeptr;
}

void InitList(Position head_ptr)
{
	head_ptr->Element = -1;
	head_ptr->Next = NULL;
}

Position AddHead(Position node_ptr) {
	Position header_ptr = (Position)malloc(sizeof(struct Node));
	header_ptr->Next = node_ptr;
	header_ptr->Element = -1;
	return header_ptr;
}
Position DeleteNode(Position list, int k)
{
	int i;
	for (i = 0; i<k - 1 && list->Next != NULL; ++i)
	{
		list = list->Next;
	}
	Position q = list->Next;
	list->Next = q->Next;
	free(q);
	return list;
	//q=NULL;
}
void PrintList(Position node_ptr)
{
	

	if (node_ptr->Next) {
		PrintList(node_ptr->Next);
		if (node_ptr->Element == -1) return;
		else {
			printf("%d", node_ptr->Element);
			return;
		}
	}
	else {
		printf("%d", node_ptr->Element);
		return;
	}
	
}


Position PushFront(Position root_ptr, int data)
{
	if (root_ptr->Element == -1) {
		root_ptr->Element = data;

	}
	else {
		Position pre_node_ptr = ApplyNode(data);
		pre_node_ptr->Next = root_ptr;
		root_ptr = pre_node_ptr;

	}
	return root_ptr;
}

void DeleteList(Position root_ptr)
{

	Position p = root_ptr;
	Position d;
	while (p->Next)
	{
		d = p;
		p = p->Next;
		free(d);
		d = NULL;
	}
}


int main() {
	int n = 0;
	int i = 0;
	char c = '0';
	int k = 0;
	int j = 0;
	int len = 0;
	List L1 = NULL;


	scanf("%d", &n);
	getchar();
	for (i = 0; i < n; i++) {
		L1 = (Position)malloc(sizeof(struct Node));
		InitList(L1);
		while ((scanf("%c", &c) != EOF) && (c != ' ')) {
			L1 = PushFront(L1, c - '0');
			len++;
		}
		L1 = AddHead(L1);
		scanf("%d", &k); getchar();
		if (k != 0) {
			DeleteNode(L1, k);
			PrintList(L1);
		}
		else if (k == 0) {
			PrintList(L1);
		}

		printf("\n");
		DeleteList(L1);
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值