Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

reverse linked list

#include <iostream>
using namespace std;

typedef struct node Node;

struct node{
    
    int data;
    Node *next;
};

Node *Create_Node(int data, Node *next){
    
    Node *new_node = (Node *) malloc(sizeof(Node));
    new_node->data = data;
    new_node->next = next;
    return new_node;
}

Node *Insert_Node(Node *head, int data){
    
    Node *new_node = Create_Node(data, NULL);
    
    if(head == NULL){
        return new_node;
    }
    Node *current_node = head;
    while(current_node->next != NULL){
        
        current_node = current_node->next;
    }
    
    current_node->next = new_node;
    
    return head;
}

Node *Reverse_LinkedList(Node *head){
    
    Node *temp = NULL;
    Node *prev_data = NULL;
    Node *current_node = head;
    
    while(current_node != NULL){
        
        prev_data = current_node;
        current_node = current_node->next;
        prev_data->next = temp;
        temp = prev_data;
    }
    head = prev_data;
    return head;
}

void Print_LinkedList(Node *head){
    
    Node *current_node = head;
    while(current_node != NULL){
        printf("%d ",current_node->data);
        current_node = current_node->next;
    }
    printf("
");
}

int main() {
	
	Node *head;
	
	head = Insert_Node(head, 10);
	head = Insert_Node(head, 20);
	head = Insert_Node(head, 30);
	
	Print_LinkedList(head);
		
	head = Reverse_LinkedList(head);
	
	Print_LinkedList(head);
	return 0;
}
Comment

linked list reverse

//iterative
public ListNode ReverseList(ListNode head) 
{
    ListNode prev = null;
    var current= head;

    while(current!=null)
    {
        var next = current.next;
        current.next=prev;
        prev =current;
        current=next;
    }
    return prev;
}
// recursive
public ListNode ReverseList(ListNode head) 
{
    if(head==null || head.next == null)
        return head;

    var t2 = ReverseList(head.next);
    head.next.next = head;
    head.next = null;

    return t2;
}
Comment

Reverse Linked List

 public ListNode ReverseList(ListNode head) 
 {
   if(head==null || head.next == null)
   	return head;

  var t = ReverseList(head.next);
  head.next.next = head;
  head.next = null;

  return t;
}
Comment

reverse linked list

def reverseLinkedList(self):
        # Complexity Time: O(n), Complexity Memory: O(1)
        # Initialize previous and current pointers
        prev, curr = None, self.head
        # loop over the linked list nodes until the current is Null 
        while curr:
        	# Store current's next node in a temporary variable 
            next = curr.next
            # Reverse current node's pointer
            curr.next = prev
            # Move pointers one position ahead
            prev = curr
            curr = next
        self.head = prev
Comment

reversing the linked list

// Java program for reversing the linked list
 
class LinkedList {
 
    static Node head;
 
    static class Node {
 
        int data;
        Node next;
 
        Node(int d)
        {
            data = d;
            next = null;
        }
    }
 
    /* Function to reverse the linked list */
    Node reverse(Node node)
    {
        Node prev = null;
        Node current = node;
        Node next = null;
        while (current != null) {
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        node = prev;
        return node;
    }
 
    // prints content of double linked list
    void printList(Node node)
    {
        while (node != null) {
            System.out.print(node.data + " ");
            node = node.next;
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        LinkedList list = new LinkedList();
        list.head = new Node(85);
        list.head.next = new Node(15);
        list.head.next.next = new Node(4);
        list.head.next.next.next = new Node(20);
 
        System.out.println("Given Linked list");
        list.printList(head);
        head = list.reverse(head);
        System.out.println("");
        System.out.println("Reversed linked list ");
        list.printList(head);
    }
}
 
// This code has been contributed by Mayank Jaiswal
Comment

reverse a linkedlist

/*method to reverse a linked list */ 
reverse(list) {
            let current = list.head;
            let prev = null;
            let next = null;
            if (this.head) {
                //only one node
                if (!this.head.next) { return this; }
                while (current) {
                    next = current.next;//store next node of current before change
                    current.next = prev;//change next of current by reverse the link
                    prev = current;//move prev node forward
                    current = next;//move current node forward
                }
                list.head = prev
                return list
            }
            return "is empty"
        }
Comment

Reverse a Linked List

	    /* Before changing next pointer of current node,
        store the next node */
        next = curr -> next
        /*  Change next pointer of current node */
        /* Actual reversing */
        curr -> next = prev
        /*  Move prev and curr one step ahead */
        prev = curr
        curr = next
Comment

reverse a linked list

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        
        ListNode current = head;
        ListNode prev = null;
        ListNode next = null;
        
        if(head == null)
        {
            return head;
        }
        while(current != null)
        {
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        return prev;
    }
}
Comment

Revese a Linked List

LinkedList<Integer> ll = new LinkedList<>();

ll.add(1);
ll.add(2);
ll.add(3);
System.out.println(ll);

LinkedList<Integer> ll1 = new LinkedList<>();

ll.descendingIterator().forEachRemaining(ll1::add);

System.out.println(ll1);
Comment

reverse linkedlist

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;  
        ListNode current = head;
    
        
        while(current != null) { 
            ListNode next = current.next; 
            current.next = prev;
            prev = current;
            current = next;
        }
       return prev; 
    }
}
Comment

reverse linkedlist

Collections.reverse(list);
Comment

Reverse a Linked List

void reverse()
	{
		node* current, * prev, * temp;
		current = head;
		prev = NULL;
		while (current != NULL)
		{
			temp = current->next;
			current->next = prev;
			prev = current;
			current = temp;
		}
		head = prev;
	}
Comment

PREVIOUS NEXT
Code Example
Csharp :: json serialization 
Csharp :: unity draw ray from one object to another 
Csharp :: unity model ripper 
Csharp :: how to add to a list in c# 
Csharp :: c# form set auto scale 
Csharp :: pause unity game 
Csharp :: prevent system shutdown c# 
Csharp :: c# normalize value 
Csharp :: How to add rigidbody as a variable 
Csharp :: c# combobox lock edit 
Csharp :: c# wpf row definition height * in code 
Csharp :: Allow edit in Datagrid C# 
Csharp :: XMLWriter write xml C# 
Csharp :: c# export datatatble to excel 
Csharp :: interface property implementation c# 
Csharp :: C# max rand 
Csharp :: c# tuple 
Csharp :: how to see if a number is even c# 
Csharp :: c# create log file 
Csharp :: c# map function 
Csharp :: c# windows forms cancel event 
Csharp :: C# System.nanoTime 
Csharp :: unity scene switch 
Csharp :: unitry raycast 
Csharp :: monogame print debug 
Csharp :: persian datapicker 
Csharp :: c# create a dummy class 
Csharp :: Show empty message in data table angular material, If no data found 
Csharp :: asp.net c# get user email address from AD 
Csharp :: or operator in shell 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =