Created
May 15, 2018 23:57
-
-
Save jianminchen/09f4370e28f728622c0c7293308ec7ec to your computer and use it in GitHub Desktop.
Swap kth node with kth to last node in a singly linked list - I got trouble to make the test case work. I cooled down after 30 minutes, went back to review past practice!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace SwapKthAndKthToLastNodeValues | |
{ | |
class Node | |
{ | |
public int Value { get; set; } | |
public Node Next { get; set; } | |
public Node(int val) | |
{ | |
Value = val; | |
} | |
} | |
/// <summary> | |
/// May 15, 2018 | |
/// swap a singly linked list kth node with kth to last node. | |
/// </summary> | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
RunTestcase(); | |
} | |
public static void RunTestcase() | |
{ | |
var root = new Node(1); | |
root.Next = new Node(2); | |
root.Next.Next = new Node(3); | |
root.Next.Next.Next = new Node(4); | |
root.Next.Next.Next.Next = new Node(5); | |
root.Next.Next.Next.Next = new Node(6); | |
var afterSwap = ReverseKthAndKthToLastInSingleLinkedList(root, 2); | |
} | |
/// <summary> | |
/// May 15, 2018 | |
/// reverse kth node and kth to last node in a singly linked list | |
/// </summary> | |
/// <param name="root"></param> | |
/// <param name="k"></param> | |
/// <returns></returns> | |
public static Node ReverseKthAndKthToLastInSingleLinkedList(Node root, int k) | |
{ | |
if (root == null || k <= 0) | |
{ | |
return null; | |
} | |
var length = getLength(root); | |
if(k > length) | |
{ | |
return root; | |
} | |
var kthNode = getKthNode(root, k); | |
var kthToLastNode = getKthToLastNode(root, k, length); | |
// swap two nodes' values instead of changing the point | |
var tmp = kthNode.Value; | |
kthNode.Value = kthToLastNode.Value; | |
kthToLastNode.Value = tmp; | |
return root; | |
} | |
public static int getLength(Node root) | |
{ | |
if (root == null) | |
{ | |
return 0; | |
} | |
var iterate = root; | |
int index = 1; // current length | |
while (iterate != null) | |
{ | |
iterate = iterate.Next; | |
index++; | |
} | |
return index; | |
} | |
/// <summary> | |
/// kth node - how to define? | |
/// </summary> | |
/// <param name="root"></param> | |
/// <param name="k"></param> | |
/// <returns></returns> | |
private static Node getKthNode(Node root, int k) | |
{ | |
var iterate = root; | |
int index = 0; | |
while (iterate != null) | |
{ | |
// iterate = iterate.Next; | |
if (index + 1 == k) | |
{ | |
return iterate; | |
} | |
iterate = iterate.Next; // bug 2: return first before iterating to the next one | |
index++; | |
} | |
return null; | |
} | |
private static Node getKthToLastNode(Node root, int k, int length) | |
{ | |
var slow = root; | |
var fast = root; | |
int index = 0; | |
while (fast != null) | |
{ | |
fast = fast.Next; | |
if (index + 1 >= k) // bug 3: > is changed to >= | |
{ | |
slow = slow.Next; | |
} | |
index++; | |
} | |
return slow; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I could not figure out what kind of bug I had. It is so wield and I could not pinpoint the place after more than one hour. I gave up, moved on reviewing past practice on linked list. I decided to work on recursive solution first. I found the bug on line 37 and 38, the linked list is writing on the same node twice! May 15, 2018 I need to be super patient to myself.