public void setPrevious(LinkListNode p) { prev = p; if (p != null && p.next != this) { p.setNext(this); } }
{//return kth to last: implement an algorithm to find the kth to last elment of a single linked list. static void Main(string[] args) { //need test later LinkListNode first = new LinkListNode(0, null, null); LinkListNode head = first; LinkListNode second = first; for (int i = 1; i < 7; i++) { second = new LinkListNode(i, null, null); first.setNext(second); second.setPrevious(first); first = second; } for (int i = 0; i <= 7 + 1; i++) { //Console.WriteLine(kthToLastEleTwoPointer(head, i).data); LinkListNode node = kthToLastEle(head, i); String nodeValue = node == null ? "null" : "" + node.data; Console.WriteLine(nodeValue); } }
//return kth to last: implement an algorithm to find the kth to last elment of a single linked list. static void Main(string[] args) { //need test later LinkListNode first = new LinkListNode(0, null, null); LinkListNode head = first; LinkListNode second = first; for (int i = 1; i < 7; i++) { second = new LinkListNode(i, null, null); first.setNext(second); second.setPrevious(first); first = second; } for (int i = 0; i <= 7 + 1; i++) { //Console.WriteLine(kthToLastEleTwoPointer(head, i).data); LinkListNode node = kthToLastEle(head, i); String nodeValue = node == null ? "null" : "" + node.data; Console.WriteLine(nodeValue); } }