给定一个单链表和一个表示链表位置的整数K ,任务是从链表的开头和结尾删除第K个节点。
例子:
Input: 1 → 2 → 3 → 4 → 5 → 6, K = 3
Output: 1 → 2 → 5 → 6
Explanation: Deleted Nodes : 3, 4
Input: 1 → 2 → 3 → 4 → 5 → 6, K = 1
Output: 2 → 3 → 4 → 5
Input: 1 → 2 → 3 → 4 → 5 → 6, K = 4
Output: 1 → 2 → 5 → 6
方法:按照步骤解决问题
- 初始化两个指针fast和slow ,以遍历链表。
- 将两个节点都指向链表的头部。
- 使用快速指针进行迭代,直到快速指向从头开始的第(K – 1)个节点。
- 遍历时,保持firstPrev存储快速指针的前一个节点。
- 现在,在每次迭代中通过节点增加慢速和快速指针,直到fast->next变得等于NULL 。
- 遍历时,保持secondPrev存储慢指针的前一个节点。
- 使用firstPrev和secondPrev指针删除链表的两个节点。
- 打印更新的链表。
下面是上述方法的实现:
C++14
// C++ implementation of the above approach
#include
#include
using namespace std;
// Structure of a
// Linked list Node
struct Node {
int data;
struct Node* next;
};
// Function to insert a new node
// at the front of the Linked List
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node
= (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
// Function to print the Linked List
void printList(struct Node* node)
{
// while node is not NULL
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
cout << endl;
}
// Function to delete nodes from
// both ends of a Linked List
Node* DeleteNodesfromBothEnds(struct Node** head_ref, int k)
{
// Empty List
if (head_ref == NULL)
return *head_ref;
// Represent nodes that remove
// the next node of each
Node* firstPrev = NULL;
Node* secondPrev = NULL;
Node* fast = *head_ref;
// copy of head_ref
Node* head = *head_ref;
// Move fast to (k - 1)
// nodes ahead of slow
for (int i = 0; i < k - 1; ++i) {
firstPrev = fast;
fast = fast->next;
}
Node* slow = *head_ref;
// Iterate until fast reaches
// end of the Linked List
while (fast != NULL && fast->next != NULL) {
secondPrev = slow;
slow = slow->next;
fast = fast->next;
}
// Remove first node
if (firstPrev == secondPrev) {
if (firstPrev == NULL) {
// Remove the head node
head = head->next;
}
else {
// Remove the middle Node
firstPrev->next = firstPrev->next->next;
}
}
else if (firstPrev != NULL && secondPrev != NULL
&& (firstPrev->next == secondPrev
|| secondPrev->next == firstPrev)) {
// If firstPrev comes first
if (firstPrev->next == secondPrev)
firstPrev->next = secondPrev->next->next;
// If secondPrev comes first
else
secondPrev->next = firstPrev->next->next;
}
else {
// Remove the head node
if (firstPrev == NULL) {
head = head->next;
}
else {
// Removethe first Node
firstPrev->next = firstPrev->next->next;
}
// Remove the head node
if (secondPrev == NULL) {
head = head->next;
}
else {
// Remove the second Node
secondPrev->next = secondPrev->next->next;
}
}
return head;
}
// Driver code
int main()
{
// Given Linked List
struct Node* head = NULL;
push(&head, 6);
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
// Given position
int K = 3;
printList(head);
// Function call to delete nodes
// from both ends of Linked List
head = DeleteNodesfromBothEnds(&head, K);
// Print the updated Linked List
printList(head);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
public class Simple {
// Stores the head and tail
// of the Linked List
Node head = null;
Node tail = null;
// Structure of a
// Linked list Node
class Node {
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
// Function to delete nodes from
// both ends of a Linked List
Node DeleteNodesfromBothEnds(int k)
{
// Empty List
if (head == null)
return head;
// Represent nodes that remove
// the next node of each
Node firstPrev = null, secondPrev = null;
Node fast = head;
// Move fast to (k - 1)
// nodes ahead of slow
for (int i = 0; i < k - 1; ++i) {
firstPrev = fast;
fast = fast.next;
}
Node slow = head;
// Iterate until fast reaches
// end of the Linked List
while (fast != null
&& fast.next != null) {
secondPrev = slow;
slow = slow.next;
fast = fast.next;
}
// Remove first node
if (firstPrev == secondPrev) {
if (firstPrev == null) {
// Remove the head node
head = head.next;
}
else {
// Remove the middle Node
firstPrev.next
= firstPrev.next.next;
}
}
else if (firstPrev != null && secondPrev != null
&& (firstPrev.next == secondPrev
|| secondPrev.next == firstPrev)) {
// If firstPrev comes first
if (firstPrev.next == secondPrev)
firstPrev.next
= secondPrev.next.next;
// If secondPrev comes first
else
secondPrev.next
= firstPrev.next.next;
}
else {
// Remove the head node
if (firstPrev == null) {
head = head.next;
}
else {
// Removethe first Node
firstPrev.next
= firstPrev.next.next;
}
// Remove the head node
if (secondPrev == null) {
head = head.next;
}
else {
// Remove the second Node
secondPrev.next
= secondPrev.next.next;
}
}
return head;
}
// Function to insert a new node
// at the end of the Linked List
public void push(int new_data)
{
Node new_node = new Node(new_data);
if (head == null) {
head = new_node;
tail = new_node;
}
else {
tail.next = new_node;
tail = new_node;
}
}
// Function to print the Linked List
public void printList()
{
Node tnode = head;
// while tnode is not NULL
while (tnode != null) {
System.out.print(tnode.data + " ");
tnode = tnode.next;
}
}
// Driver Code
public static void main(String[] args)
{
// Given Linked List
Simple llist = new Simple();
llist.push(1);
llist.push(2);
llist.push(3);
llist.push(4);
llist.push(5);
llist.push(6);
// Given position
int K = 3;
// Function call to delete nodes
// from both ends of Linked List
llist.DeleteNodesfromBothEnds(K);
// Print the updated Linked List
llist.printList();
}
}
C#
// C# implementation of the approach
using System;
public class Simple {
// Stores the head and tail
// of the Linked List
public Node head = null;
public Node tail = null;
// Structure of a
// Linked list Node
public class Node {
public int data;
public Node next;
public Node(int d)
{
data = d;
next = null;
}
}
// Function to delete nodes from
// both ends of a Linked List
public Node DeleteNodesfromBothEnds(int k)
{
// Empty List
if (head == null)
return head;
// Represent nodes that remove
// the next node of each
Node firstPrev = null, secondPrev = null;
Node fast = head;
// Move fast to (k - 1)
// nodes ahead of slow
for (int i = 0; i < k - 1; ++i) {
firstPrev = fast;
fast = fast.next;
}
Node slow = head;
// Iterate until fast reaches
// end of the Linked List
while (fast != null
&& fast.next != null) {
secondPrev = slow;
slow = slow.next;
fast = fast.next;
}
// Remove first node
if (firstPrev == secondPrev) {
if (firstPrev == null) {
// Remove the head node
head = head.next;
}
else {
// Remove the middle Node
firstPrev.next
= firstPrev.next.next;
}
}
else if (firstPrev != null && secondPrev != null
&& (firstPrev.next == secondPrev
|| secondPrev.next == firstPrev)) {
// If firstPrev comes first
if (firstPrev.next == secondPrev)
firstPrev.next
= secondPrev.next.next;
// If secondPrev comes first
else
secondPrev.next
= firstPrev.next.next;
}
else {
// Remove the head node
if (firstPrev == null) {
head = head.next;
}
else {
// Removethe first Node
firstPrev.next
= firstPrev.next.next;
}
// Remove the head node
if (secondPrev == null) {
head = head.next;
}
else {
// Remove the second Node
secondPrev.next
= secondPrev.next.next;
}
}
return head;
}
// Function to insert a new node
// at the end of the Linked List
public void push(int new_data)
{
Node new_node = new Node(new_data);
if (head == null) {
head = new_node;
tail = new_node;
}
else {
tail.next = new_node;
tail = new_node;
}
}
// Function to print the Linked List
public void printList()
{
Node tnode = head;
// while tnode is not NULL
while (tnode != null) {
Console.Write(tnode.data + " ");
tnode = tnode.next;
}
}
// Driver Code
public static void Main(String[] args)
{
// Given Linked List
Simple llist = new Simple();
llist.push(1);
llist.push(2);
llist.push(3);
llist.push(4);
llist.push(5);
llist.push(6);
// Given position
int K = 3;
// Function call to delete nodes
// from both ends of Linked List
llist.DeleteNodesfromBothEnds(K);
// Print the updated Linked List
llist.printList();
}
}
// This code contributed by shikhasingrajput
输出:
1 2 5 6
时间复杂度: O(N)
空间复杂度: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live