In-place Reversal of a LinkedList

I'm going to cover all the LeetCode Patterns for coding Interviews mentioned by a few folks. I'll write the code using C#. Today's post is related to In-place Reversal of a LinkedList. If anyone sees any area of improvement please feel free to add.


To reverse a LinkedList, we need to reverse one node at a time. We will start with a pointer current which will initially point to the head of the LinkedList and a pointer previous which will point to the previous node that we have to process. Initially previous will be pointed to null.

We will reverse the head node by pointing it to the previous before moving on to the next node. Also, we will update the previous to always point to the previous node that we have processed.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace LeetCodePattren

{

internal class InPlaceReversalLinkedList

{

public class Node

{

public Node next;

public int val;

public Node(int val)

{

this.val = val;

this.next = null;

}

}

public Node ReverseLinkedListInPlace(Node head)

{

if (head == null) {

return null;

}

if (head.next == null) {

return head;

}

//current pointer point out to head

Node current = head;

// in starting previous node is null

Node previous = null;

while (current != null)

{

// moving current node to next

current=head.next;

//pointing head to previous node

head.next = previous;

// moving previous to head

previous = head;

// moving head to current

head = current;

}

return previous;

}

public static void Main(string[] args)

{

Node node=new Node(5);

node.next = new Node(9);

node.next.next = new Node(10);

node.next.next.next = new Node(11);

node.next.next.next.next= new Node(12);

InPlaceReversalLinkedList inPlaceReversalLinkedList = new InPlaceReversalLinkedList();

Node node1 =inPlaceReversalLinkedList.ReverseLinkedListInPlace(node);

while (node1 != null) {

Console.WriteLine(node1.val);

node1=node1.next;

}

Console.ReadLine();

}

}

}


要查看或添加评论,请登录

Karambir Sharma的更多文章

  • Monotonic Stack

    Monotonic Stack

    I will cover all the LeetCode Patterns for coding Interviews mentioned by a few folks. I'll write the code using C#.

  • Cyclic Sort

    Cyclic Sort

    I'm going to cover all the LeetCode Patterns for coding Interviews mentioned by a few folks. I'll write the code using…

  • Fast and Slow Pointers

    Fast and Slow Pointers

    I will cover all the LeetCode Patterns for coding Interviews mentioned by a few folks. I'll write the code using C#.

  • LeetCode Patterns for Coding Interviews:

    LeetCode Patterns for Coding Interviews:

    I'm covering all the LeetCode Patterns for coding Interviews mentioned by a few folks. I'll write the code using C#.

  • #programmingworld#problemsolvingskills#leetcode#overcomefear

    #programmingworld#problemsolvingskills#leetcode#overcomefear

    I am always thinking about how to improve my coding skills and understanding of the problems in the programming world…

社区洞察

其他会员也浏览了