cs: add “206. Reverse Linked List”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
1ce3f94173
commit
880aaa9179
1 changed files with 25 additions and 0 deletions
25
cs/reverse-linked-list.cs
Normal file
25
cs/reverse-linked-list.cs
Normal file
|
@ -0,0 +1,25 @@
|
|||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* public int val;
|
||||
* public ListNode next;
|
||||
* public ListNode(int val=0, ListNode next=null) {
|
||||
* this.val = val;
|
||||
* this.next = next;
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
public class Solution {
|
||||
public ListNode ReverseList(ListNode head) {
|
||||
ListNode next = null;
|
||||
|
||||
while (head != null) {
|
||||
var toRight = head.next;
|
||||
head.next = next;
|
||||
next = head;
|
||||
head = toRight;
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue