Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Reverse linked list iterative method
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//Title of this code //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 { public static void Main(string[] args) { Node head = null; LinkedList.Append(ref head, 25); LinkedList.Append(ref head, 5); LinkedList.Append(ref head, 18); LinkedList.Append(ref head, 7); Console.WriteLine("Linked list:"); LinkedList.Print(head); LinkedList.Reverse(ref head); Console.WriteLine(); Console.WriteLine("Reversed Linked list:"); LinkedList.Print(head); } public static class LinkedList { public static void Append(ref Node head, int data) { if (head != null) { Node current = head; while(current.Next != null) { current = current.Next; } current.Next = new Node(); current.Next.Data = data; } else { head = new Node(); head.Data = data; } } public static void Print(Node head) { if (head == null) return; Node current = head; do { Console.Write("{0} ", current.Data); current = current.Next; } while(current != null); } public static void Reverse(ref Node head) { if (head == null) return; Node prev = null, current = head, next = null; while( current.Next != null ) { next = current.Next; current.Next = prev; prev = current; current = next; } current.Next = prev; head = current; } } public class Node { public int Data = 0; public Node Next = null; } } }
Show compiler warnings
[
+
]
Show input
Compilation time: 0.08 sec, absolute running time: 0.06 sec, cpu time: 0.06 sec, average memory usage: 14 Mb, average nr of threads: 3
edit mode
|
history
|
discussion
Linked list: 25 5 18 7 Reversed Linked list: 7 18 5 25