// C# implementation to modify the  
// contents of the linked list 
using System; 
  
class GFG 
{ 
      
/* Linked list node */
public class Node 
{ 
    public int data; 
    public Node next; 
}; 
  
/* Function to insert a node at  
the beginning of the linked list */
static Node push(Node head_ref,  
                  int new_data) 
{ 
    /* allocate node */
    Node new_node = new Node(); 
      
    /* put in the data */
    new_node.data = new_data; 
          
    /* link the old list at the end  
    of the new node */
    new_node.next = head_ref;  
          
    /* move the head to point to the new node */
    head_ref = new_node; 
      
    return head_ref; 
}  
  
static Node front, back; 
  
/* Split the nodes of the given list  
into front and back halves, 
and return the two lists  
using the reference parameters. 
Uses the fast/slow pointer strategy. */
static void frontAndBackSplit( Node head) 
{ 
    Node slow, fast; 
      
    slow = head; 
    fast = head.next; 
      
    /* Advance 'fast' two nodes, and  
    advance 'slow' one node */
    while (fast != null) 
    { 
        fast = fast.next; 
        if (fast != null) 
        { 
            slow = slow.next; 
            fast = fast.next; 
        } 
    } 
      
    /* 'slow' is before the midpoint in the list,  
        so split it in two at that point. */
    front = head; 
    back = slow.next; 
    slow.next = null; 
} 
  
/* Function to reverse the linked list */
static Node reverseList(Node head_ref) 
{ 
    Node current, prev, next; 
    current = head_ref; 
    prev = null; 
    while (current != null) 
    { 
        next = current.next; 
        current.next = prev; 
        prev = current; 
        current = next; 
    }  
    head_ref = prev; 
    return head_ref; 
} 
  
// perform the required subtraction operation  
// on the 1st half of the linked list 
static void modifyTheContentsOf1stHalf() 
{ 
    Node front1 = front, back1 = back; 
      
    // traversing both the lists simultaneously 
    while (back1 != null) 
    { 
        // subtraction operation and node data 
        // modification 
        front1.data = front1.data - back1.data; 
          
        front1 = front1.next; 
        back1 = back1.next; 
    } 
} 
  
// function to concatenate the 2nd(back) list  
// at the end of the 1st(front) list and  
// returns the head of the new list 
static Node concatFrontAndBackList(Node front, 
                                   Node back) 
{ 
    Node head = front; 
      
    if(front == null) 
        return back; 
      
    while (front.next != null) 
        front = front.next;  
          
    front.next = back; 
      
    return head; 
} 
  
// function to modify the contents of 
// the linked list 
static Node modifyTheList(Node head) 
{ 
    // if list is empty or contains  
    // only single node 
    if (head == null || head.next == null) 
        return head; 
    front = null; back = null; 
      
    // split the list into two halves 
    // front and back lists 
    frontAndBackSplit(head); 
          
    // reverse the 2nd(back) list 
    back = reverseList(back); 
      
    // modify the contents of 1st half  
    modifyTheContentsOf1stHalf(); 
      
    // agains reverse the 2nd(back) list 
    back = reverseList(back); 
      
    // concatenating the 2nd list back to the  
    // end of the 1st list 
    head = concatFrontAndBackList(front, back); 
  
    // pointer to the modified list 
    return head; 
} 
  
// function to print the linked list 
static void printList( Node head) 
{ 
    if (head == null) 
        return; 
      
    while (head.next != null) 
    { 
        Console.Write(head.data + " -> "); 
        head = head.next; 
    } 
    Console.WriteLine(head.data ); 
} 
  
// Driver Code 
public static void Main() 
{ 
    Node head = null; 
      
    // creating the linked list 
    head = push(head, 10); 
    head = push(head, 7); 
    head = push(head, 12); 
    head = push(head, 8); 
    head = push(head, 9); 
    head = push(head, 2); 
      
    // modify the linked list 
    head = modifyTheList(head); 
      
    // print the modified linked list 
    Console.WriteLine( "Modified List:" ); 
    printList(head); 
} 
} 
  
// This code is contributed by PrinciRaj1992

C相关代码片段

reddit photoshop will be deactivated popup macos

uppercase to lowercase in c

show available value

lowercase to uppercase in C

c switch return

how to change hte middle node in linked list C

openGL create shape

script to check program running and restart

printf("")

Rewind to a commit Git

mouse click for colab

how to remove newline in the end of fgets string

fit text to conatiner flutter

gorm string array

js observe url change

clock skew detected makefile

time() function in c

worst fit code in c

first fit code in c

roem evaluation pyspark

calculator c

safet algorith im c

c copy char*

jfug function

soil moisture sensor interfacing with ESP32

flutter sidebar menu example

access images in c panel htaccess file

c include header file in the folder aboce

c include libray

gcc name output file

C data link escape char

How to type DLE in C

vscode Ctrl+d undo

C[strtol] func

pointer c++

power in c

sort three numbers

strip in c

c time duration

overleaf itemize a b c

route groups

itoa c code

queue in c

'wsgirequest' object has no attribute 'get'

c copy file

how to print array data in html using javascript

hello world program in c

how to compare a variable with a string in c

c print array

c rand range

exponents without pow c

how to reversed number in c

c random number with range

pthread mutex destroy while lock

pthread init mutex twice

insert c or p

woocommerce align add to cart buttons

vscode fold all code when open file

commit github non funziona

makefile both c and c++

scanf with space

c macro variable arguments

c printf format

check if prime c

read file line by line c

c use define in string

pattern program in c

Write the code in C to implement selection sort

c program of Goldbach conjecture

otput only one decimal point in c

trim string c

tokenize string c

firebase message in app doesn't work

implementing stack using linked list

esercizi semplici con le funzioni in c

array to linked list in c

sorted array to bst implementation in c

doubly linked list insertion in the middle

arch distro

distance vector routing algorithm