Problem B: Remove Nth Node From End of List
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 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;
}