Skip to content

Commit

Permalink
Merge pull request #955 from min8grace/main
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Jul 1, 2024
2 parents f885f1b + d225467 commit 16ef53c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
35 changes: 35 additions & 0 deletions davemin/2024-06-24-1721-Swapping-Nodes-in-a-Linked-List.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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 SwapNodes(ListNode head, int k) {
ListNode firstNode = head;
ListNode secondNode = head;

while(k-1>0){
firstNode = firstNode.next;
k--;
}

// while keeping k-gap, move two node parallelly till the first node ends.
ListNode kStepAhead = firstNode;
while(kStepAhead.next!= null){
kStepAhead = kStepAhead.next;
secondNode = secondNode.next;
}
// swap two nodes.
int temp = firstNode.val;
firstNode.val = secondNode.val;
secondNode.val = temp;

return head;
}
}
35 changes: 35 additions & 0 deletions davemin/2024-06-24-61-Rotate-List.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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 RotateRight(ListNode head, int k) {
if (head == null)
{
return null;
}
ListNode tail = head;
int length = 1;
while(tail.next != null){
Console.WriteLine(tail.val);
tail = tail.next;
length++;
}
tail.next = head;
k = length - k % length;
while(k>0){
head = head.next;
tail = tail.next;
k--;
}
tail.next = null;
return head;
}
}

0 comments on commit 16ef53c

Please sign in to comment.