Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Linked list solution with remove
//Linked list problem solution //Rextester.Program.Main is the entry point for your code. Don't change it. using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { // main public static void Main(string[] args) { //Your code goes here //Console.WriteLine("Hello, world!"); LinkedList myList = new LinkedList(); myList.AddHead(null); Console.WriteLine(myList.Length().ToString()); myList.AddHead("one"); Console.WriteLine(myList.Length().ToString()); myList.AddHead("two"); Console.WriteLine(myList.Length().ToString()); } // ListNode class - contains the data, and the next pointer public class ListNode { public object Data {get;set;} public ListNode Next {get;set;} public ListNode(object data) { this.Data = data; } } // The LinkedList class public class LinkedList { // The head of the list public ListNode Head {get;set;} // The LinkedList constructor public LinkedList() { } // Add a node to the head of the list with // the data public bool AddHead(object data) { bool result = false; ListNode newNode = new ListNode(data); if(newNode != null) { result = true; if(this.Head == null) { this.Head = newNode; } else { ListNode temp = this.Head; this.Head = newNode; newNode.Next = temp; } } return result; } /// <summary> /// Remove the node corresponding to the number passed in /// ** zero based index ** /// </summary> /// <param name="nodeNumber"></param> /// <returns></returns> public bool RemoveNode(int nodeNumber) { bool removed = false; ListNode current = this.Head; ListNode prev = null; int index = 0; while(index < nodeNumber && current != null) { prev = current; current = current.Next; index++; } if(current != null) { if(prev == null) { this.Head = current.Next; } else { prev.Next = current.Next; current = null; } current = null; removed = true; } return removed; } // Return the length of the list public int Length() { int length = 0; ListNode thisNode = Head; if(thisNode != null) { while(thisNode.Next != null) { length++; thisNode = thisNode.Next; } } return length; } } } }
run
|
edit
|
history
|
help
0
Ligma Bofa Joe
n th to last element
GFV SDXZASX
obrazek
test
temp1
2
Beadando Simon Szabolcs 10/M
Square Integer Matrix
ssz